Flutter: How to save widget state in ListView.builder - flutter

I have an app which has a ListView.builder() which builds widgets. Those widgets fetch an image from firebase cloud storage. Every time those widgets go off the screen and come back, the widget fetches the image from firebase cloud storage again. I want to make it so that it doesn't fetch the image again but rather displays the image it already got when it is rebuilt if the widget already got the image.
It currently looks like this: https://cln.sh/S9Ky0L.

Related

How to rebuild Widget after updating data to RTDB flutter

Hi I'm new to flutter and I'm currently trying to work with Realtime Database from firebase. I manage to get all my data from RTDB and I also made function to edit the data in RTDB.
The problem is I cannot make my Widget to rebuild when I already done updating the RTDB data.
Here's how the process of the Data fetching and updating goes:
First, We will have variable A on screen A to fetch the RTDB data in the initState
Second, the fetched data will be used to create Widget of Text in Screen A
Third, there will be a button on screen A to push the screen to Screen B (Editing Page)
Fourth, After the user done editing it will push the user into Splash Screen and it will be Pop back to screen A
Fifth (Here's the problem), the Widget still build the old data, not the new data that has been edited.
I thought after step 4, Screen A will do the fetching again in initState.. Am I wrong ? How can I do the rebuild of widget ? Thanks before

Can I store a widget from widget and later use it in another page without rebuilding it from scratch? - Flutter

I have a Stateful widget called Widget1 shown in one page. When I open my app the widget1 gets build from scratch and shown in the page. Let's assume the widget1 is quite heavy with lot's of other widget's inside it.
Now I want to display the same widget in another page but I don't want to build the same widget1 from scratch like I did in the first page. I want to reuse the widget which was built in the Previous page to be shown in this page.
Is that possible in flutter? I know flutter must build a widget inorder to make changes in Ui. But is this really possible? that is , to store a widget and use it, whenever we want without having to rebuild it, again from scratch?.

How to send back an image variable from one screen to another - Flutter

I am new to flutter and I am currently working on a project that uses cameras internally.This screen has two image variables that need to receive an image from the camera and store them easily.
Right now the camera screen is closed like this:
onPressed: ()=> Navigator.pop(context,Image.file(File(imagePath)))
How do I change the Image variables from the previous screen?
I see you are passing a widget Image.file(File(imagePath)) to result parameter of Navigator.pop. So in this case, the code where you open the camera screen using Navigator.push , you can assign the return value of Navigator.push to some variable because the return type is Future<T>.
final imgWidget = await Navigator.push<Image>(...)
Flutter has no concept of different screens. Everything is Widgets and you have a Widget tree. Between straight Provider, Riverpod, Bloc and GetX there are a variety of different state management solutions that allow you to store information over the level of pages.

How to prevent Stateful Widget stored in List to be disposed?

Let me start by explaining the problem. I have several buttons which are created based on data I'm getting from a server. On each button click I need to create and display a widget which will present me some data (this widget is also built dynamically). The state of this widget has to be preserved during the lifetime of the app. (for example if I click another button and show a different widget, I need to be able to click on the first button and show the first widget in its preserved state). Number of buttons can also be changed during the app lifetime.
I tried using IndexedStack to achieve this, but when the number of buttons is changed I need to add "pages" to IndexedStack, therefore I need to recreate a new IndexedStack which will have some of my old widgets, so I pull widgets from a List or create new ones if needed. This works great, except Flutter calls dispose() method on my widgets which are stored in the list. I tried using the AutomaticKeepAliveClientMixIn but it didn't help.
I'm guessing this has something to do that the widgets get detached from the parent, but when I reattach them to new parent (new Indexed stack) Flutter doesn't figure out this properly.
Any ideas?
Try to store data to local storage or sqlite for persistence data.

how to avoid rebuilding duplicate widgets when navigating back to previous page in flutter?

When the user logs in, they get pushed to the homepage and their data is retrieved from database on initstate and then the widgets are built. However, i noticed for the widgets that are built without a streambuilder, where i manually retrieve data once, are duplicated if i navigate to another page and then come back. When I say duplicated I mean the exact same data is redundantly copied again using the same widget, so like a users' profile pic could show up twice. I am using a listview most of the time for these cases/
How do i avoid this and just built the widgets once so if the user goes back the page stays the same?