Question:
I want to show a progress indicator until the required data is fetched from the server. Currently what I am doing is made a function getQuotes() that will fetch the data into a variable using setState(). And the Used the FutureBuilder where its future parameter is set to getQuotes(). But this approach gives me a non-ending CircularProgressIndicator. I don’t why it is happening. Is ther any problem with the combination of FutureBuilder() and setState() ? Can Some one help ?Here is my code,
Answer:
Another solution would be to make yourgetQuotes()
function returning a Future<String>
instead of a Future<void>
and then access the data via the snapshot instead of accessing the state.The Flutter docs of the
FutureBuilder
Flutter Docs are also doing it that way in the demo. As long as you don’t need the state of userInfo
in other places this should be an acceptable solution and you could also remove userInfo
as variable. If you want to maintain or manipulate it later you could try to put the setState({})
statement in the ConnectionState.done
switch case within an if(snapshot.hasData){}
block.If you have better answer, please add a comment about this, thank you!