【问题标题】:flutter, iterate over documents in fire base collection颤动,迭代火基集合中的文档
【发布时间】:2021-12-29 21:15:05
【问题描述】:

我正在尝试获取集合中的所有文档并像这样迭代它们:

class InitialRecipe extends StatelessWidget {
  final UID;
  CollectionReference collection =
      FirebaseFirestore.instance.collection("recipes");

  InitialRecipe(this.UID, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return FutureBuilder<QuerySnapshot>(
      future: collection.get(), //.doc('VRWodus2pN2wXXHSz8JH').get(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) {
          return Text("Something went wrong");
        }

        snapshot.data!.docs.forEach((e) {
          print(e.data.toString());
          print(e.data.runtimeType);
        });

        return Loading();
      },
    );
  }
}

我拿不到文件。相反,我收到一条消息,告诉我 snapshot.data 的返回值为 null。

但是,我可以像这样从集合中获取一个特殊的文档:

FirebaseFirestore.instance.collection("recipes").doc('VRWodus2pN2wXXHSz8JH').get()

我做错了吗?

如何遍历“recipes”集合中的文档??

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    您的AsyncSnapshot 似乎尚未完成加载。

    return FutureBuilder<QuerySnapshot>(
      future: collection.get(), //.doc('VRWodus2pN2wXXHSz8JH').get(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) {
          return Text("Something went wrong");
        }
    
        if (snapshot.hasData) { // ?
          snapshot.data!.docs.forEach((e) {
            print(e.data.toString());
            print(e.data.runtimeType);
          });
        }
    
        return Loading();
      },
    );
    

    【讨论】:

    • 谢谢!就是这样。
    猜你喜欢
    • 2021-11-12
    • 2021-03-19
    • 2021-05-07
    • 2020-01-11
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 2021-08-09
    • 2021-12-23
    相关资源
    最近更新 更多