How to rebuild Widget after updating data to RTDB flutter - 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

Related

Flutter Dropdown Button not saving state during Screen Navigation

Hi I am running into an issue where I am unable to save the state of the DropdownButton (the text shown in the Dropdown Button itself, which is the value that I have selected). I am using GetX for state navigation.
Due to the way my code has been structured, when I navigate from one screen to a second one, and wish to navigate back, I am utilising Get.to(First() instead of Get.back().
Here is my logic:
I will save the value the user has selected from the DropdownButton into SharedPreferences. This value will also be immediately shown to the user on the button itself due to the nature of the button.
When user navigates away (e.g. he is now on the second screen), and visits first screen again, I will load the selected value from SharedPreferences and construct the DropdownButton item such that this value will be shown at the top.
The issue I am facing:
Retrieving data from SharedPreferences is asynchronous, so I don't know how to construct the FirstScreen again while I am waiting for data to be retrieved from SharedPreferences. Could anyone help me out? Thank you!
Use GetStorage() instead of shared-preference . No Async issue
dependencies:
get_storage: ^2.0.3
use GetStorage through an instance or use directly GetStorage().read('key')
final box = GetStorage();
To write information you must use write :
box.write('quote', 'GetX is the best');
To read values you use read:
print(box.read('quote'));
// out: GetX is the best

Flutter: How to save widget state in ListView.builder

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.

How to prefetch data from api for 'ShowModalBottomSheet' even before it being opened in Flutter

Hi I'm trying to use ShowModalBottomSheet in my flutter App. But right now how it's being worked is the data which need to be displayed is being fetched only when ShowModalBottomSheet code executed and which creating a delay.
I want to know how do I prefetch the data beforhand using Future so that which can be used instantly when ShowModalBottomSheet triggered.
You could use and create a provider or bloc, and load the data, whenever the application's starts or the page is opened.
This way, the data will be available before the bottom sheet opens.

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?

Flutter refreshing data without screen flickering and scroll issues

I have a page that presents chat messages and has the dates of a group of messages as a divider in my flutter app. The data is pulled from localDB and I am trying to make it refresh the data by polling the DB for updates, however everytime I rebuild the stream or even future it makes the screen flicker and it automatically scrolls to the top of the list. The code is too big for here so I have posted it here.
It uses a Stream builder that calls a widget with data it gets from the first stream and uses another streambuilder to build the messages. I am sure this has something to do with it, but have no idea how to change that or fix the flicker and scroll weirdness.
Hoping someone has some ideas of how to handle this better or fix the flickering.
What you could try is creating a "app state" for each page, for example:
Loading app state - which displays a Progress Indicator when you're polling from DB/server.
Success app state - this dismisses the Progress Indicator and just shows an empty container, displaying your data.
Failed app state - Displays an error icon of your choice.
Perhaps you could try this library, Flutter GetX , and I recommend this library because of how simple it is to use.
It has state management library, which saved me lots of time and code using streams and setState({}).