颤振:显示在一段时间内调用了null的getter文档

时间:2020-05-13 08:36:30

标签: firebase flutter dart google-cloud-firestore

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

获取器“ documents”在null上被调用。

这是我的代码。

class chats extends StatelessWidget { 
@override
  Widget build(BuildContext context) {
child: Container(child:getlast())
}}

这是肉食

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

1 个答案:

答案 0 :(得分:2)

使用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