未来功能中的SetState

时间:2019-01-21 23:19:01

标签: asynchronous dart flutter future setstate

我希望为将来的功能实现自动同步。我尝试了一个setstate,但是它不能正常工作。你有想法吗?很高兴提出建议。

   Future<AlgoliaQuerySnapshot> queryFunc()  async{

   AlgoliaQuery query = algolia.instance.index('groups').setAroundLatLng('51.5078845,7.4702625');
   Future<AlgoliaQuerySnapshot> snap  = query.getObjects();
   return snap;}

1 个答案:

答案 0 :(得分:0)

此代码是如何构建一个等待异步代码的小部件的示例。

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

也请阅读official docs