Home > Mobile >  The method 'map' isn't defined for the type 'Object' from streamtransformer
The method 'map' isn't defined for the type 'Object' from streamtransformer

Time:01-11

Im following a tutorial and here is original code which is without any error:

  static StreamTransformer transformer<T>(
      T Function(Map<String, dynamic> json) fromJson) =>
      StreamTransformer<DataSnapshot, List<T>>.fromHandlers(
        handleData: (DataSnapshot data, EventSink<List<T>> sink) {
          final snaps = data.value.map((doc) => doc.data()).toList(); // there is no map
          final objects = snaps.map((json) => fromJson(json)).toList();// there is no map

          sink.add(objects);
        },
      );

and when I copy paste to my code, and here is the error: The method 'map' isn't defined for the type 'Object', could you please let me know why it happens or what can I do to exchange? thanks !

CodePudding user response:

Here you have the repo link: https://github.com/JohannesMilke/todo_app_firestore_example

It has the same issue you have: https://github.com/JohannesMilke/todo_app_firestore_example/issues/1

In utils.dart replace it with:

static StreamTransformer transformer(
T Function(Map<String, dynamic> json) fromJson) =>
StreamTransformer<QuerySnapshot<Map<String, dynamic>>, List>.fromHandlers(
handleData: (QuerySnapshot <Map<String, dynamic>> data, EventSink<List> sink) {
final snaps = data.docs.map((doc) => doc.data()).toList();
final objects = snaps.map((json) => fromJson(json)).toList();
sink.add(objects);
},
);

In firebaseApi.dart replace it with:

static Stream<List> readTodos() => FirebaseFirestore.instance
.collection('todo')
.orderBy(TodoField.createdTime, descending: true)
.snapshots()
.transform(Utils.transformer(Todo.fromJson)
as StreamTransformer<QuerySnapshot<Map<String, dynamic>>, List>);
  •  Tags:  
  • Related