Home > Back-end >  The argument type 'Map<dynamic, dynamic>' can't be assigned to the parameter ty
The argument type 'Map<dynamic, dynamic>' can't be assigned to the parameter ty

Time:01-04

Map<String, dynamic> messageInfoMap = {
    "message": message,
    "sendBy": myUserName,
    "ts": lastMessageTs,
    "imgUrl": myProfilePic
  };

and....

Future addMessage(
  String chatRoomId, String messageId, Map messageInfoMap) async {
return FirebaseFirestore.instance
    .collection("chatrooms")
    .doc(chatRoomId)
    .collection("chats")
    .doc(messageId)
    .set(messageInfoMap);

for the last line .set(messageInfoMap), I am getting an error saying "The argument type 'Map<dynamic, dynamic>' can't be assigned to the parameter type 'Map<String, dynamic>'" I don't see anything wrong, as messageInfoMap should be 'Map<String, dynamic>', but I don't know why I'm getting this error.

CodePudding user response:

You have two variables, first, you declared messageInfoMap somewhere (you don't show where but I presume all this code is inside the same class?)

Then, your function takes three arguments, the third argument is also called messageInfoMap, and its type is Map, which of course is the same as Map<dynamic, dynamic>. I presume you are passing the first variable as an argument on the function? If that is the case, you just have to make sure you make the second variable a map of string, dynamic:

Future addMessage(
  String chatRoomId, String messageId, Map<String, dynamic> messageInfoMap) async {
return FirebaseFirestore.instance
    .collection("chatrooms")
    .doc(chatRoomId)
    .collection("chats")
    .doc(messageId)
    .set(messageInfoMap);

  •  Tags:  
  • Related