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.

Share:
618
Alexandra
Author by

Alexandra

Updated on December 08, 2022

Comments

  • Alexandra
    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
      diegoveloper over 5 years
      use FutureBuilder Widget
  • Alexandra
    Alexandra over 5 years
    Hi Oswin, thanks for your Feedback. But do you have an idea for a SetState? I need the data refresh automatically.
  • Oswin Noetzelmann
    Oswin Noetzelmann over 5 years
    The futurebuilder is called automatically when the query finishes. Did you try it out?