corvus
corvus

Reputation: 1

Flutter : The argument type 'Object?' can't be assigned to the parameter type 'Map<String, dynamic>'

I just tried to fetch messages from firebase, but it says that DocumentSnapShot returning an Object, my ModelMessage.fromJson want it to be Map<String, dynamic>..

FirebaseFirestore firestore = FirebaseFirestore.instance;

  Stream<List<MessageModel>> getMessagesByUserId(int userId) {
    try {
      return firestore
          .collection('messages')
          .where('userId', isEqualTo: userId)
          .snapshots()
          .map((QuerySnapshot list) {
        var result = list.docs.map<MessageModel>((DocumentSnapshot message) {
          

          return MessageModel.fromJson(message.data()); <--- the error is here (message.data())

        }).toList();

       
        result.sort(
          (MessageModel a, MessageModel b) =>
              a.createdAt.compareTo(b.createdAt),
        );

        return result;
      });
    } catch (e) {
      throw Exception(e);
    }
  }

Is there some way to convert from object to dynamic string or there is something wrong on my code?..Thank you

Upvotes: 0

Views: 111

Answers (1)

Ahmed Saeed
Ahmed Saeed

Reputation: 106

try this

 return MessageModel.fromJson(message.data() as Map<String, dynamic>);

Upvotes: 1

Related Questions