Flutter The argument type 'StreamTransformer<dynamic, dynamic>' can't be assigned to the parameter type StreamTransformer<QuerySnapshot List<Message>>
974
Add QuerySnapshot<Map<String, dynamic>>
as your return type. worked for me.
static StreamTransformer<QuerySnapshot<Map<String, dynamic>>, List<T>> transformer<T>(
T Function(Map<String, dynamic> json) fromJson) =>
StreamTransformer<QuerySnapshot<Map<String, dynamic>>, List<T>>.fromHandlers(
handleData: (QuerySnapshot<Map<String, dynamic>> data, EventSink<List<T>> sink) {
final snaps = data.docs.map((doc) => doc.data()).toList();
final users = snaps.map((json) => fromJson(json)).toList();
sink.add(users);
},
);
Author by
Al Mamun
Updated on December 30, 2022Comments
-
Al Mamun over 1 year
I am trying to build a chat application using Flutter & Firebase I took below code from Github and used it on my project. On an earlier version of Flutter, it was working, on 2.0 it is showing the following error:
The argument type 'StreamTransformer<dynamic, dynamic>' can't be assigned to the parameter type 'StreamTransformer<QuerySnapshot, List<Message>>'
The error is triggered by the following line:
.transform(Utils.transformer(User.fromJson));
Full code:
class FirebaseApi { static Stream<List<User>> getUsers() => FirebaseFirestore.instance .collection('users') // .collection('ch ats') .orderBy(UserField.timestamp, descending: true) .snapshots() .transform(Utils.transformer(User.fromJson)); static Future uploadMessage(String idUser, String message) async { final refMessages = FirebaseFirestore.instance.collection('chats/$idUser/messages'); final newMessage = Message( // idUser: myId, idUser: Globals.auth.currentUser!.uid, // urlAvatar: myUrlAvatar, urlAvatar: ChatPageState.photoUrl.toString(), // username: myUsername, username: ChatPageState.displayName.toString(), message: message, createdAt: DateTime.now(), ); await refMessages.add(newMessage.toJson()); final refUsers = FirebaseFirestore.instance.collection('users'); await refUsers.doc(idUser).update({UserField.timestamp: DateTime.now()}); } static Stream<List<Message>> getMessages(String idUser) { return FirebaseFirestore.instance .collection('chats/$idUser/messages') .orderBy(MessageField.createdAt, descending: true) .snapshots() .transform(Utils.transformer(Message.fromJson)); } } class Utils { static StreamTransformer transformer<T>( T Function(Map<String, dynamic> json) fromJson) => StreamTransformer<QuerySnapshot, List<T>>.fromHandlers( handleData: (QuerySnapshot data, EventSink<List<T>> sink) { final snaps = data.docs.map((doc) => doc.data()).toList(); final users = snaps.map((json) => fromJson(json)).toList(); sink.add(users); }, ); static DateTime toDateTime(Timestamp value) { // if (value == null); return value.toDate(); } static dynamic fromDateTimeToJson(DateTime date) { if (date == null) return null; return date.toUtc(); } }
-
Patrick Waweru almost 3 yearsInstead of transform can you try
snapshots.map((e){ // add logo }) .....
-
Al Mamun almost 3 years@Patrick Waweru Hi, I tried to add snapshots.map((e){ // add logo }) it's not working, can you pls elaborate and add it to my code for better understanding?
-
The Anonymous Koder almost 3 yearsYou can try casting
-
Al Mamun almost 3 years@ The Anonymous Koder how i can do that? can you pls make an example using my code.
-
Patrick Waweru almost 3 years@AlMamun then share your models like this Message so that can see your model and share an example using map instead of transform
-
Al Mamun almost 3 years@Patrick Waweru, I am sorry! whcih model you are taking about?
-
-
Mahmoud Eidarous almost 2 yearsVery explanatory. Thank you so much!