When working with Futures and FutureBuilders, it is recommended not to execute the Future methods inside the FutureBuilder.

I learned it the hard way!

Read on to know why ๐Ÿ‘‡๐Ÿป

#flutter #flutterdev
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
ใ€€

Keep Current with Aswin Gopinathan ๐Ÿ’™๐Ÿ‡ฎ๐Ÿ‡ณ

Aswin Gopinathan ๐Ÿ’™๐Ÿ‡ฎ๐Ÿ‡ณ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @GopinathanAswin

14 Dec
Unit Testing in Flutter 101

A mega thread ๐Ÿงต

#flutter #flutterdev
Unit Testing is the most important testing method, that is usually performed by the developer.

Unit Testing tests each individual modules (method, function or a class) for its proper functioning.
This indirectly means you have to maintain loose coupling in your application.

That is, each function or a method should be able to perform only one functionality.

This way it will be easier to test and even debug you codebase.
Read 19 tweets
13 Dec
In my latest tweet, I talked about how you can prevent multiple calls to your future methods using a variable approach.

What if I say, there is one more efficient approach to it!

Thanks to @DmytroGladkyi for telling me about it

Read on to know about it ๐Ÿ‘‡๐Ÿป

#flutterdev #flutter
The approach is using the AsyncMemoizer class.

To use the class, import the following package: Image
Now, letโ€™s create an instance of the AsyncMemoizer class, and initialise it inside the initState method (or you can initialise during declaration). Image
Read 7 tweets
12 Dec
Future v/s Streams

A restaurant analogy thread ๐Ÿงต๐Ÿ‘‡๐Ÿป

#flutter #flutterdev
Imagine you go to a restaurant and your date gives you two options:

1. Takeout and eat at your home

2. Find a table and eat there itself

Well these options in the Flutter world means:

1. Future

2. Stream
Okay! Lets see the first option: Takeout

You ordered:

1. Burgers ๐Ÿ”
2. Fries ๐ŸŸ
3. Pizza ๐Ÿ•

The waiter gave you a token and asked you to wait.

<Few moments later>

Your takeaway is ready and you both leave the place.
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(