简体   繁体   English

参数类型“对象?” 不能分配给参数类型 'Map<dynamic, dynamic> '</dynamic,>

[英]The argument type 'Object?' can't be assigned to the parameter type 'Map<dynamic, dynamic>'

I have an error on the "snap.snapshot.value" parameter in the following line "var map = Map <String, dynamic>.from (snap.snapshot.value);".我在以下行“var map = Map <String, dynamic>.from (snap.snapshot.value);”中的“snap.snapshot.value”参数有错误。 The error is "The argument type 'Object?'错误是“参数类型‘对象?’ can't be assigned to the parameter type 'Map <dynamic, dynamic>'. "不能分配给参数类型 'Map <dynamic, dynamic>'。”

class _HomePageState extends State<HomePage> {

  List<Posts> postsList = [];


  @override
  void initState() {

    super.initState();


    DatabaseReference postsRef = FirebaseDatabase.instance.reference().child("Posts");

    postsRef.once().then((snap) {

      var map = Map<String, dynamic>.from(snap.snapshot.value); <--- the error is here

      postsList.clear();

      map.forEach((key, value) {
        var values = Map<String,dynamic>.from(map);
        Posts posts = Posts
          (
            values['url'],
            values['descrizione'],
            values['data'],
            values['ora']
        );
        postsList.add(posts);
      });

Would you change a code like below?您会更改如下代码吗?

From

var map = Map<String, dynamic>.from(snap.snapshot.value);

To

Map<String, dynamic> map = snap.snapshot.value as Map<String, dynamic>

This is because Map.from function accepts a Map<dynamic, dynamic> but you are passing the object instead.这是因为Map.from function 接受Map<dynamic, dynamic>但您传递的是object So to pass as the same use casting like below:所以要像下面一样使用铸造:

var map = Map<String, dynamic>.from(snap.snapshot.value as Map<dynamic, dynamic>);

In your Posts model, you need to remove any explicit object casting.在您的帖子 model 中,您需要删除任何明确的 object 转换。 eg change from例如从

`Posts.fromMap(Map<String, dynamic> map) {
comments: List<String>.from((map['comments'] as List<String>)) 
}`

to

`Posts.fromMap(Map<String, dynamic> map) {
comments: List<String>.from((map['comments'] as List)) 
}`

so it's cast as a List and no farther所以它被当作一个列表,没有更远的

暂无
暂无

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

相关问题 Flutter Cloud Firestore:错误:参数类型“对象? Function()' 无法分配给参数类型 'Map<string, dynamic> '</string,> - Flutter Cloud Firestore: error: The argument type 'Object? Function()' can't be assigned to the parameter type 'Map<String, dynamic>' 参数类型 'Object? Function()' 无法分配给参数类型 'Map<string, dynamic> '</string,> - The argument type 'Object? Function()' can't be assigned to the parameter type 'Map<String, dynamic>' 错误:参数类型 'Map<string, dynamic> ?' 不能分配给参数类型 'Map<dynamic, dynamic> '</dynamic,></string,> - Error: The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<dynamic, dynamic>' Flutter:参数类型“对象?” 不能分配给参数类型 'Map<string, dynamic> '</string,> - Flutter : The argument type 'Object?' can't be assigned to the parameter type 'Map<String, dynamic>' Flutter 参数类型“对象?” 不能分配给参数类型 'Map<string, dynamic> '</string,> - Flutter The argument type 'Object?' can't be assigned to the parameter type 'Map<String, dynamic>' 参数类型“对象?” 不能分配给参数类型 'Map<string, dynamic> ' 使用 DataSnapshot 从我的模型中获取数据</string,> - The argument type 'Object?' can't be assigned to the parameter type 'Map<String, dynamic>' Using DataSnapshot to get data from my models 参数类型 'Future<dynamic> ' 不能分配给参数类型 'String'</dynamic> - The argument type 'Future<dynamic>' can't be assigned to the parameter type 'String' 错误:参数类型'StreamTransformer<dynamic, dynamic> ' 不能分配给参数类型 'StreamTransformer <querysnapshot*, list<todo> &gt;</querysnapshot*,></dynamic,> - error: The argument type 'StreamTransformer<dynamic, dynamic>' can't be assigned to the parameter type 'StreamTransformer<QuerySnapshot*, List<Todo>> 参数类型“列表<dynamic> ' 不能分配给参数类型 'string?'。 在文档 snapShot flutter 中</dynamic> - the argument type 'list<dynamic>' can't be assigned to the parameter type 'string?'. In document snapShot flutter 错误:参数类型 'Future<dynamic> ' 不能分配给参数类型 'void Function()?'</dynamic> - Error: The argument type 'Future<dynamic>' can't be assigned to the parameter type 'void Function()?'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM