firebase - flutter :显示在一段时间内调用了null的getter文档

标签 firebase flutter dart google-cloud-firestore

我正在从无状态小部件调用方法。本文档从firebase数据库查询。但是当它在输出中显示错误一段时间后,便显示结果。我如何才能停止那3秒钟的错误。这是显示的错误

The getter "documents" was called on null.



这是我的代码。
class chats extends StatelessWidget { 
@override
  Widget build(BuildContext context) {
child: Container(child:getlast())
}}

这就是肉ho
getlast(){
    var groupChatId = 123456789;
    var id = chat.UID;
      return FutureBuilder (
        future: Firestore.instance
            .collection("messages")
            .document(groupChatId)
            .collection(groupChatId)
            .orderBy("timestamp", descending: true)
            .limit(1)
            .getDocuments(),
        builder: (context,AsyncSnapshot snapshot) {
          var con = snapshot.data.documents[0].data["content"];
         Container(
                  height: 27,
                  child: Text("You: $con",
                      overflow: TextOverflow.fade,
                      style:  TextStyle(
                        fontSize: 20),
                   )});
            }

最佳答案

使用FutureBuilder时,您需要检查connectionState:

          FutureBuilder(
            future: Firestore.instance
            .collection("messages")
            .document(groupChatId)
            .collection(groupChatId)
            .orderBy("timestamp", descending: true)
            .limit(1)
            .getDocuments(),
            builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                var con = snapshot.data.documents[0].data["content"];
              Container(
                  height: 27,
                  child: Text("You: $con",
                      overflow: TextOverflow.fade,
                      style:  TextStyle(
                        fontSize: 20),
                   )});
              } else if (snapshot.connectionState == ConnectionState.none) {
                return Text("No data");
              }
              return CircularProgressIndicator();
            },
          ),
documents在null上被调用,这意味着仍未检索到数据,因此通过使用connectionState.done,您可以确保已检索到数据。

https://api.flutter.dev/flutter/widgets/ConnectionState-class.html

关于firebase - flutter :显示在一段时间内调用了null的getter文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61769922/

相关文章:

ios - 如何将用户添加到 Firebase 数据库

android - 如何在flutter mobile中保存网站以供离线使用

dart - 从开头或结尾修剪 Dart 字符串中的空格

flutter - 在 Flutter 中更新对象实例属性的最佳实践是什么?该实例嵌套在提供程序类的 map 中,如下所示

node.js - 将 firebase 数据 Node 导出为 pdf 报告

java - SingleValueEvent 显示为空

ios - 快速显示所有 firebase 用户的帖子

android - Flutter:应用程序在热重载后不断回到初始路线

firebase - 在 Flutter 中从 Firebase 读取数组

flutter - 什么时候应该在 Dart/Flutter 中使用分号?