Reputation: 1
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
Reputation: 106
try this
return MessageModel.fromJson(message.data() as Map<String, dynamic>);
Upvotes: 1