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:
Now, let’s create an instance of the AsyncMemoizer class, and initialise it inside the initState method (or you can initialise during declaration).
Now, lets write the Future method.

(I will let you know why I gave the print statement)
So, we used the runOnce() method of the AsyncMemoizer instance to register an async method which will be run only once, even if we call the method multiple times !!

This way you can execute the future method inside the FutureBuilder without worrying about efficiency.

Proof 👇🏻
Now create a button which will trigger a setState. Notice the print statement. It gets executed only once (initially), even though the FutureBuilder is re-rendered again when you click the button!

This proves the async code is run only once in the entire lifetime of your screen.
This way we can avoid any complexities that could arise by using a variable approach.

For small applications, the variable approach won’t have much side-effects, but for production-level applications, this approach is preferred.

• • •

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
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
Read 10 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!

:(