Every time your UI refreshes (eg: using a setState), the future method is run again, which can turn out to be very expensive!
Sometimes this could be helpful, but most of the times itโs a curse for the application.
So how can you fix it ?
๐๐ป
We need to set the future in such a way that its run only once, and will never run again until you re-open the screen or you explicitly define a Swipe-down-to-refresh feature.
So, what is that one thing in Flutter, which will run only once in the entire life-cycle of your screen
Itโs the initState method !!
Now, lets tweak our code a bitโฆ
We now use a variable approach.
We create a Future variable and store the Future method inside that variable when the initState block runs.
And now, we use that variable inside our FutureBuilder!
Now, even if the screen state changes, the future method wonโt be run again.
This way there is less load on your application as you are running the async code only once!
You can explicitly define a refresh button to manually refresh the data (run the async code again).
Trust me, I learned it the hard way!
But, as they sayโฆ We learn from our mistakes. Right?
If this thread was helpful, do give it a like and retweet so that it can reach more wonderful developers like you!
๐
Donโt forget to hit the follow button @GopinathanAswin . I will be sharing more insights as I learn new things in Dart and Flutter.
I am not the best out there, I am just an ordinary dev who loves to learn something new everyday and share it with the wonderful community! ๐
โข โข โข
Missing some Tweet in this thread? You can try to
force a refresh