12

迁移到 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());
        });
  }

添加显示此错误的空检查 (!) 后<the getter 'docs' is not defined for the type of object>

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

4 回答 4

12

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

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

我们可以streamStreamBuilder.

StreamBuilder<QuerySnapshot>(
  ...
);

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

snapshot.data!.docs.length
于 2021-03-17T09:56:52.837 回答
7

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

 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());
    });
 }
于 2021-06-07T08:07:09.963 回答
6

有几个解决方案:

  • 为您提供一个类型StreamBuilder

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

    builder: (context, QuerySnapshot snapshot)
    
  • 用于as垂头丧气:

    (snapshot.data! as QuerySnapshot).docs['key']
    
于 2021-05-23T09:25:03.017 回答
0

添加错误和连接状态检查,如下所示:

      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"]);
                    });
              });
        }
于 2021-03-17T09:42:21.917 回答