简体   繁体   中英

The argument type 'Map<dynamic, dynamic>' can't be assigned to the parameter type 'Map<String, dynamic>'. error showing in flutter

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM