在Flutter中,当文档数据包含Map值时,如何使用where子句?

omhiaaxx  于 2022-11-17  发布在  Flutter
关注(0)|答案(1)|浏览(142)


我正在学习firebase并尝试创建一个聊天应用程序...
我点击用户列表,检查他的聊天室是否已经创建了。

  • 清单项目

登录的用户或没有....为此,我创建了一个未来的函数...但显示了一个错误..看起来它拒绝使用[ breaker with where子句....如果是这样,那么如何获得Map的键的值...通常我使用varmap['key']。

Future<ChatroomModel?> getchatroom(UserModel targetuser) async {
    ChatroomModel? temp;
    QuerySnapshot querysnapshot = await FirebaseFirestore.instance
        .collection('chatrooms')
        .where("participants[${widget.usermodel.userid}]", isEqualTo: true)
        .where("participants[${targetuser.userid}]", isEqualTo: true)
        .get();

    if (querysnapshot.docs.length > 0) {
     
      print("already exists");
    } else {
     

      print("Created new one");
    }

    return temp;
  }

这是我聊天室模特

class ChatroomModel
{
  String? chatroomid;
  Map<String,dynamic>? participants;//{'xdsfsdf':true, 'afdafd':true}
  String? lastmessage;

  ChatroomModel({this.chatroomid, this.participants,this.lastmessage});

  ChatroomModel.fromMap(Map<String, dynamic> map) {
    chatroomid=map['chatroomid'];
    participants=map['participants'];
    lastmessage=map['lastmessage'];
  }

  Map<String,dynamic> toMap()
  {
    return {
      'chatroomid':chatroomid,
      'participants':participants,
      'lastmessage':lastmessage,
    };
  }

}

获取以下错误

[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: 'package:cloud_firestore_platform_interface/src/field_path.dart': Failed assertion: line 50 pos 16: '!path.contains('[')': Paths must not contain '~', '*', '/', '[', or ']'.
fkaflof6

fkaflof61#

试试看:

QuerySnapshot querysnapshot = await FirebaseFirestore.instance
        .collection('chatrooms')
        .where("participants." + widget.usermodel.userid, isEqualTo: true)
        .where("participants." + targetuser.userid, isEqualTo: true)
        .get();

相关问题