【问题标题】:futureBuilder can not get data from snapshotfutureBuilder 无法从快照中获取数据
【发布时间】:2020-09-10 20:42:58
【问题描述】:
 Future fetchData() async{
              List<Profile> list=[];
              list = await Firestore.instance
              .collection("users").getDocuments().then((querySnapshot){
               querySnapshot.documents.forEach((document) {
               list.add( Profile(
                    photos: [document['photoUrl'],],
                    name: document['nickname'],
                    age: 2,
                    distance: 2,
                    education: '3',
                    bio:"a"
                ));

            });
            print(list);
            print('list');
            return list; //[Instance of 'Profile']
        });
}



class _MainControllerState extends State<MainController> {







@override
Widget build(BuildContext context) {
  return new Container(
    padding: EdgeInsets.fromLTRB(0.0, 60.0, 0.0, 30.0),
    child: FutureBuilder(
      future: fetchData(),
      builder: (
        BuildContext context,
        AsyncSnapshot snapshot,
      ){
        if (snapshot.connectionState == ConnectionState.done ) {
           if (snapshot.data == null) {
             print(snapshot);//AsyncSnapshot<dynamic>(ConnectionState.done, null, null)
        return Text('no data');

      } else {
         print('matches');
              print(snapshot);
             List<Match> matches = snapshot.data
              .map((Profile profile) => Match(profile: profile))
              .toList();

          return CardStack(
            matchEngine: MatchEngine(matches: matches),
          );
      }

        } else if (snapshot.error) {
          print('matches1');
        } else {
          print('matches2');
        }
        return CardStack();
      },

    ),

  );
}

}

当我打印出列表时,我可以得到 ['Profile' 的实例],但是当我在快照中打印数据时,我只能得到 AsyncSnapshot(ConnectionState.done, null, null) ,我该如何解决这个错误?

我尝试将 snapshot.connectionState == ConnectionState.done 更改为 snapshot.has 数据。但它不起作用。

【问题讨论】:

    标签: firebase flutter dart


    【解决方案1】:
     Future<List<Profile>> fetchData() async{
              List<Profile> list=[];
              list = await Firestore.instance
              .collection("users").getDocuments().then((querySnapshot)  async {
               querySnapshot.documents.forEach((document) {
               list.add( Profile(
                    photos: [document['photoUrl'],],
                    name: document['nickname'],
                    age: 2,
                    distance: 2,
                    education: '3',
                    bio:"a"
                ));
    
            });
    
            print(list);
            print('list');
            return list; //[Instance of 'Profile']
        });
    
            return list; //[Instance of 'Profile']
            }
    

    【讨论】:

      【解决方案2】:

      FutureBuilder 不知道你从fetchData() 返回什么,所以你必须指定你将返回一个List&lt;Profile&gt;。因为没有类型的 FutureFuture&lt;void&gt; 相同。

      替换:

      Future fetchData() async{...}
      

      与:

      Future<List<Profile>> fetchData() async{...}
      

      类似地在FutureBuilder中指定

      代替:

      builder: (
          BuildContext context,
          AsyncSnapshot snapshot,
        ){...}
      

      这样做:

      builder: (
          BuildContext context,
          AsyncSnapshot<List<Profile>> snapshot,
        ){...}
      

      希望这会有所帮助。

      【讨论】:

      • 我试过了,但它也返回 AsyncSnapshot>(ConnectionState.done, null, null)
      • @ZivCheung 您必须从获取数据中返回 null。正如我已经测试了上面的代码,它应该可以正常工作。
      • 我试过你的代码,但它仍然返回颤动:['Profile'实例,'Profile'实例]颤动:列表颤动:AsyncSnapshot>(ConnectionState.done,null , null)
      • @ZivCheung 你在 dartpad 中测试过吗?
      猜你喜欢
      • 2021-11-17
      • 2023-04-10
      • 2016-12-28
      • 2020-12-27
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      相关资源
      最近更新 更多