简体   繁体   English

在 Flutter / Dart 和 Sound Null 中检索 firebase firestore 文档

[英]Retrieving firebase firestore documents in Flutter / Dart and Sound Null

I am working my way through a Udemy Flutter class and I am in a chapter dealing with Firebase.我正在学习 Udemy Flutter 课程,我正在学习处理 Firebase 的章节。 The class is about 3-4 years old and it seems is just old enough that the sample completed code crashes when accessing the Firebase portions.这个类大约有 3-4 年的历史,它似乎足够老了,以至于在访问 Firebase 部分时示例完成的代码崩溃了。 I started a new project from scratch and cobbled bits and pieces to get it mostly up and running but I have now hit a dead end.我从头开始了一个新项目,并拼凑了一些零碎的东西来让它大部分启动和运行,但我现在已经走到了死胡同。 The new project and the firebase plugins are Sound Null and the class code is not.新项目和 firebase 插件是 Sound Null 而类代码不是。 Getting and printing data from the database worked fine until I tried getting it hooked up to a stream.从数据库获取和打印数据工作正常,直到我尝试将其连接到流。 I am specifically running into a problem iterating over the received documents.我特别遇到了迭代接收到的文档的问题。 When I set up the message variable using (the Flutter) snapshot.data in a for-in loop and try to iterate over the returned documents (#1) I have a null problem.当我在 for-in 循环中使用(Flutter)snapshot.data 设置消息变量并尝试迭代返回的文档(#1)时,我遇到了空问题。 Without specifying the type (#1a) as AsyncSnapshot the for-in loop errors that I cannot iterate over a non nullable.如果没有将类型 (#1a) 指定为 AsyncSnapshot,我无法在不可为空的情况下迭代 for-in 循环错误。 Changing the type gets rid of the compile time error but generates a runtime error of "type _JsonQuerrySnapshot is not a subtype of type Iterable".更改类型可以消除编译时错误,但会生成“类型 _JsonQuerrySnapshot 不是 Iterable 类型的子类型”的运行时错误。 Dart is not my primary language and I have been pulling my hair out googling this for several hours to no avail. Dart 不是我的主要语言,我已经在谷歌上搜索了几个小时但无济于事。 Any help is appreciated.任何帮助表示赞赏。

final _firestore = FirebaseFirestore.instance;最终 _firestore = FirebaseFirestore.instance; //<-----earlier in the code //<-----在代码的前面

      children: <Widget>[

        StreamBuilder<QuerySnapshot>(
          stream: _firestore.collection('messages').snapshots(),
          builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) { //<----Problem #1a here
            List<Text> messageWidgets = [];
            if (snapshot.hasData) {

              final messages = snapshot.data;

              for (var message in messages) { //<----------Problem #1 here
                final messageText = message.data['text'];
                final messageSender = message.data['sender'];
                final messageWidget =
                    Text('$messageText from $messageSender');
                messageWidgets.add(messageWidget);
              }
            }
            return Column(
              children: messageWidgets,
            );
          },
      

Well, after another hour or so of googling around I found the right place to look.好吧,又过了一个小时左右的谷歌搜索,我找到了合适的地方。 The docs for the flutter plugin for firestore have a massive difference from the course I am taking. firestore 的 flutter 插件的文档与我正在学习的课程有很大的不同。 Reading(ctrl-c, ctrl-v) them I'm at least able to mostly understand and have the program back on track to finish my course.阅读(ctrl-c,ctrl-v)它们我至少能够大致理解并使程序回到正轨以完成我的课程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM