Alexandra
Alexandra

Reputation: 79

SetState in Future-function

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;}

Upvotes: 1

Views: 688

Answers (1)

Oswin Noetzelmann
Oswin Noetzelmann

Reputation: 9545

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.

Upvotes: 1

Related Questions