SetState in Future-function
618
This code is an example for how to build a widget that waits for your async code.
Widget mywidget = new FutureBuilder(
future: queryFunc(),
builder: (BuildContext context, AsyncSnapshot<AlgoliaQuerySnapshot> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.active:
case ConnectionState.waiting:
return Text("not loaded yet");
case ConnectionState.done:
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
return Text(snapshot.data.foo); // success - build whatever UI elements you need
}
return null;
});
Also read the official docs.
Author by
Alexandra
Updated on December 08, 2022Comments
-
Alexandra over 1 year
I like to have an autosync for my future function. I tried a setstate but it did not work properly. Do you have an idea? Happy about suggestions.
Future<AlgoliaQuerySnapshot> queryFunc() async{ AlgoliaQuery query = algolia.instance.index('groups').setAroundLatLng('51.5078845,7.4702625'); Future<AlgoliaQuerySnapshot> snap = query.getObjects(); return snap;}
-
diegoveloper over 5 yearsuse FutureBuilder Widget
-
-
Alexandra over 5 yearsHi Oswin, thanks for your Feedback. But do you have an idea for a SetState? I need the data refresh automatically.
-
Oswin Noetzelmann over 5 yearsThe futurebuilder is called automatically when the query finishes. Did you try it out?