简体   繁体   English

没有为 streamtransformer 中的“对象”类型定义方法“地图”

[英]The method 'map' isn't defined for the type 'Object' from streamtransformer

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?当我将粘贴复制到我的代码时,这是错误:方法'map'没有为'Object'类型定义,你能告诉我为什么会发生这种情况或者我可以做些什么来交换? thanks !谢谢 !

Here you have the repo link: https://github.com/JohannesMilke/todo_app_firestore_example在这里你有回购链接: 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它有同样的问题: https://github.com/JohannesMilke/todo_app_firestore_example/issues/1

In utils.dart replace it with:在 utils.dart 中将其替换为:

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:在 firebaseApi.dart 将其替换为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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