Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to use where clause when document data contains map value in flutter?

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’].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Try this:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading