属性 'docs' 无法无条件访问,因为received可以'null' Flutter

The property 'docs' cannot be unconditionally accessed because received can be 'null' Flutter

迁移到 null-safety 后显示此错误。我现在该怎么办?

  Widget chatMessages() {
    return StreamBuilder(
        stream: messageStream,
        builder: (context, snapshot) {
          return snapshot.hasData
              ? ListView.builder(
                  padding: EdgeInsets.only(bottom: 70, top: 16),
                  itemCount: snapshot.data.docs.length,
                  reverse: true,
                  itemBuilder: (context, index) {
                    DocumentSnapshot ds = snapshot.data.docs[index];
                    return chatMessageTitle(
                        ds["message"], myUserName == ds["sendBy"]);
                  })
              : Center(child: CircularProgressIndicator());
        });
  }

添加空检查 (!) 后显示此错误

              itemCount: snapshot.data!.docs.length,
              reverse: true,
              itemBuilder: (context, index) {
                DocumentSnapshot ds = snapshot.data!.docs[index];

像这样添加错误和连接状态检查:

      Widget chatMessages() {
        return StreamBuilder(
            stream: messageStream,
            builder: (context, snapshot) {
              if (snapshot.hasError) {
                return Text('Something went wrong');
                }
              if (snapshot.connectionState == ConnectionState.waiting) {
                return Text("Loading");
                }
              return ListView.builder(
                  padding: EdgeInsets.only(bottom: 70, top: 16),
                  itemCount: snapshot.data.docs.length,
                  reverse: true,
                  itemBuilder: (context, index) {
                    DocumentSnapshot ds = snapshot.data.docs[index];
                    return chatMessageTitle(
                    ds["message"], myUserName == ds["sendBy"]);
                    });
              });
        }

您必须将 snapshot.data 转换为它的类型。假设类型是 QuerySnapshot(用 snapshot.data 的实际类型更改)。

(snapshot.data! as QuerySnapshot).docs.length

我们可以在 StreamBuilder.

中指定 stream 的类型,而不是在所有位置进行类型转换
StreamBuilder<QuerySnapshot>(
  ...
);

现在 snapshot.data 被推断为 QuerySnapshot 并且不需要类型转换。

snapshot.data!.docs.length

有几个解决方案:

  • 为您的StreamBuilder提供类型:

    StreamBuilder<QuerySnapshot> (...)
    
  • builder 的第二个参数提供类型:

    builder: (context, QuerySnapshot snapshot)
    
  • 使用as向下转型:

    (snapshot.data! as QuerySnapshot).docs['key']
    

我已经通过添加 StreamBuilder 和 builder(context, AsyncSnapshot snapshot) 解决了我的问题。

 Widget chatMessages() {
     return StreamBuilder<QuerySnapshot>(
        stream: messageStream,
        builder: (context, AsyncSnapshot snapshot) {
       return snapshot.hasData
          ? ListView.builder(
              padding: EdgeInsets.only(bottom: 70, top: 16),
              itemCount: snapshot.data.docs.length,
              reverse: true,
              itemBuilder: (context, index) {
                DocumentSnapshot ds = snapshot.data.docs[index];
                return chatMessageTitle(
                    ds["message"], myUserName == ds["sendBy"]);
              })
          : Center(child: CircularProgressIndicator());
    });
 }