Zohaib Hamdule
Zohaib Hamdule

Reputation: 658

InitState with setState or FutureBuilder? Which method is better to display data from a database in Flutter?

My app displays data gathered from a SQLite database in a ListView. I could use a FutureBuilder to wait for the data to be fetched and then return a ListView. Or I could set a callback to when my data is loaded in InitState() and then setState() with the data loaded. I was able to use both ways and display a CircularProgressIndicator until data was loaded. Which of these ways is better that I should continue to use in the rest of the app?

Upvotes: 0

Views: 113

Answers (1)

Aleksandar
Aleksandar

Reputation: 1588

FutureBuilder is a widget that builds only its children, whereas setState inside a callback block in initState does the rebuild of the entire screen. It is always better to rebuild only portion of the widget tree to gain on performance, than to rebuild entire screen when data is received.

Upvotes: 2

Related Questions