Flutter / Firestore:类“ QuerySnapshot”没有实例获取器“ document”

时间:2020-06-30 18:05:21

标签: firebase flutter dart google-cloud-firestore

我有一个应用,我想要检索数据,这些数据是Firestore数据库中以uid文档表示的消息,如此处所述,这些消息的存储方式如下: ChatRoom-> chatRoomId-> chat-> uid->消息

但我收到此错误:

在构建StreamBuilder时引发了以下NoSuchMethodError(脏,状态:_StreamBuilderBaseState #56cb5):类“ QuerySnapshot”没有实例 获取“文档”。接收方:“ QuerySnapshot”实例已尝试 调用:文档

相关的引起错误的小部件是:StreamBuilder 文件:///Users/ahmedhussain/Downloads/khamsat/Client%20Apps/HPX-KSA/hpx_ksa/lib/Screens/messages.dart:21:12 引发异常时,这是堆栈: #0 Object.noSuchMethod(dart:core-patch / object_patch.dart:53:5) #1 _MessagesState.chatRoomList。 (软件包:hpxksa / Screens / messages.dart:25:38)

这是我的代码:

class _MessagesState extends State<Messages> {
  
  Stream chatRoomsStream;

  Widget chatRoomList(){
    return StreamBuilder(
      stream: chatRoomsStream,
      builder: (context, snapshot){
        return snapshot.hasData ? ListView.builder(
            itemCount: snapshot.data.document.length,
            itemBuilder: (context, index){
              return ChatRoomTile(
                 username: snapshot.data.documents[index].data["chatRoomId"]
                      .toString().replaceAll("_", "").replaceAll(Constants.myName, "replace"),
                  chatRoomId:snapshot.data.documents[index].data["chatRoomId"]
              );
            }) : Container();
      }
      );
  }

  getUserInfogetChats() {
    DatabaseService().getChatRooms(Constants.myName).then((value) {
      setState(() {
        chatRoomsStream = value;
      });
    });
  }

  @override
  void initState() {
    getUserInfogetChats();
    super.initState();
  }


@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: chatRoomList(),
    );
  }
}


class ChatRoomTile extends StatelessWidget {
  final String username;
  final String chatRoomId;
  ChatRoomTile({this.username, this.chatRoomId});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        Navigator.push(context, MaterialPageRoute(builder: (context)=>Conversation(chatRoomId: chatRoomId,)));
      },
      child: Container(
        color: Colors.black26,
        padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
        child: Row(
          children: <Widget>[
            Container(
              height: 40,
              width: 40,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: kPrimaryColor,
                borderRadius: BorderRadius.circular(40),
              ),
              child: Text("${username.substring(0,1).toUpperCase()}"),
            ),
            SizedBox(width: 8,),
            Text(username),
          ],
        ),
      ),
    );
  }
}

这是我的get函数,用于检索包含用户名的聊天记录:

 getChatRooms(String username)async{
    return await Firestore.instance.collection("ChatRoom").
    where("users", arrayContains: username).
    snapshots();

  }

2 个答案:

答案 0 :(得分:1)

您收到的错误非常清楚是什么问题。 QuerySnapshot没有document属性。您可能打算使用documents属性,该属性与您尝试使用ListView更加一致。

snapshot.data.document更改为snapshot.data.documents的实例将解决此特定问题。

答案 1 :(得分:1)

  return StreamBuilder(
      stream: chatRoomStream,
      builder: (context, snapshot) {
        return snapshot.hasData
            ? ListView.builder(
                itemCount: snapshot.data.docs.length,
                itemBuilder: (context, index) {
                  return ChatRoomTile(
                      **snapshot.data.docs[index].data()['chatRoomId']**);
                },
              )
            : Container();
      },
    );