How to Correctly use Flutter's Future Builder Widget

Code The Web · 43K views · 2019 · 14:42

Open on YouTube

Software (other)tutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering Flutter questions about FutureBuilder rebuilding, re-running network calls, or showing stale data, since it shows how to initialize the future in initState.

Recommended by 5 people across 5 different months, 2020 to 2021.

What it is

A roughly 15 minute Flutter tutorial by Garrett Love of the Code The Web channel on using the FutureBuilder widget correctly. It explains why the future should be created once, outside build, rather than inside the builder call.

What people said

Recommends hewa jalal · 2020-07 · Stack Overflow · on “Is there any way to use FutureBuilder(future) inside the initState on this situation?”

i am not sure what you want, but i am sure my answer is the correct way of obtaining a state from an async method without causing it to be called for each re-build, you can take a look at this video [this video] for more details.

Recommends London Tran · 2020-10 · Stack Overflow · on “How to call method only after async method finishes”

Thank you to those who have responded to my question and for recommending the use of StreamBuilder. I used StreamBuilder to overcome the issue from my Original Post. I needed to add three parts to my code: 1) Future futureData; 2) futureData = getData(); 3) FutureBuilder( future: futureData, builder: (context, snapshot) { if (graphValues.isEmpty) { return new Container(); } else return Graph(closingTimesAndPrices: graphValues); }) I used futureData for the value of future: in the FutureBuilder to signal that I am waiting on futureData ( getData(); ) to fully complete before I return anything. In my Original Post, I mentioned that graphValues were returning null before getData() was finished

Recommends Calvin Gonsalves · 2021-01 · Stack Overflow · on “Flutter list of stateBuilder issues”

related to your second issue with the like button, you are probably making an unwanted network call. You should initialize your futures in the `initState` , this video explains it well : link [this video]

Recommends Farkhod · 2021-03 · Stack Overflow · on “Flutter search through data displayed in listview”

Here the problem is with FutureBuilder . In order to avoid unwanted refreshes you need to first declare your Future and then in your initState you should call the future: Future myFuture; @override void initState() { super.initState(); myFuture = _fetchData(); } ... FutureBuilder( future: myFuture, ... You can find more detailed info in this video regarding your problem: [this video]

Recommends Ketan Jain · 2021-11 · Stack Overflow · on “Flutter - Futurebuilder showing data only after reload”

You need to create a variable outside the build method and initialize it in void init() inside setState. And then pass it in the FutureBuilder's future parameter. You can see the following tutorial: [this video]