【问题标题】:Flutter/ Firestore : Class 'QuerySnapshot' has no instance getter 'document'Flutter/Firestore:“QuerySnapshot”类没有实例获取器“document”
【发布时间】:2020-10-21 02:52:02
【问题描述】:

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

但我收到此错误:

在构建 StreamBuilder(dirty, state: _StreamBuilderBaseState#56cb5): 类 'QuerySnapshot' 没有实例 吸气剂'文档'。接收方:“QuerySnapshot”实例已尝试 调用:文档

相关的导致错误的小部件是:StreamBuilder file:///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),
          ],
        ),
      ),
    );
  }
}

这是我获取包含用户名的聊天记录的函数:

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

  }

【问题讨论】:

  • 你有什么问题?
  • 此错误:“QuerySnapshot”类没有实例获取器“文档”。接收方:“QuerySnapshot”实例尝试调用:文档
  • 这不是问题。你想达到什么目的?整个问题只是一条错误消息和代码。
  • 我正在尝试根据文档 ID 从 Firestore 中检索数据
  • 您能否进一步说明您如何在 Fire Store 中构建数据

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

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

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

【讨论】:

  • 怎么会是完全相同的错误? document 不应存在于代码中的任何位置。请重新发布此错误。
  • 这是我在将其更改为 docs 后收到的内容:“QuerySnapshot”类没有实例获取器“docs”。接收方:“QuerySnapshot”实例尝试调用:文档
  • 您使用的是哪个软件包? cloud_firestore?或firebase?
  • 假设您使用的是cloud_firestore,我已经编辑了我的答案。这是一个简单的错字。
  • cloud_firestore
【解决方案2】:
  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();
      },
    );

【讨论】:

  • 将“documents”更改为“docs”并将“data['chatRoomId']​​”更改为“data()['chatRoomId']​​”
  • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
猜你喜欢
  • 2020-04-07
  • 2020-04-11
  • 2021-04-27
  • 2022-06-15
  • 1970-01-01
  • 2021-04-18
  • 2020-09-24
  • 1970-01-01
相关资源
最近更新 更多