Home > Software engineering >  how to use where clause when document data contains map value in flutter?
how to use where clause when document data contains map value in flutter?

Time:11-14

enter image description hereI am learning firebase and trying to create a chat app...

I tap on the user list to check whether him chatroom is already created with l

  • List item

logged user or not....and for that, I have created a future function...but showing an error..looks like it denies to use [ breaker with where clause....if it is so, then how to get the value of the map's key...generally I use, 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;
  }

this is my chatroommodel

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,
    };
  }


}


getting following error

[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 ']'.

CodePudding user response:

Try this:

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