参数类型 'dynamic' 无法分配给参数类型 'Map<String, dynamic>'
The argument type 'dynamic' can't be assigned to the parameter type 'Map<String, dynamic>'
final dataResponse = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
Album.fromJson(jsonDecode(dataResponse.body));
在启用 nullsafety 的项目上,Album.fromJson(jsonDecode(dataResponse.body));
此代码抛出错误 The argument type 'dynamic' can't be assigned to the parameter type 'Map<String, dynamic>'.
已关注官方doc
以下代码用于数据建模。
class Album {
final int userId;
final int id;
final String title;
Album({
required this.userId,
required this.id,
required this.title,
});
factory Album.fromJson(Map<String, dynamic> json) {
return Album(
userId: json['userId'],
id: json['id'],
title: json['title'],
);
}
}
只需要将 (Map json) 更改为 (dynamic json)
factory Album.fromJson(dynamic json) {
return Album(
userId: json['userId'],
id: json['id'],
title: json['title'],
);
}
模型已经不错了...
试试这个。
final dataResponse = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
List<dynamic> list =jsonDecode(dataResponse.body);
return list.map((e)=>Album.fromJson(e)).toList();
你只需要投射你的jsonDecode
Album.fromJson(jsonDecode(dataResponse.body));
显式类型:
Album.fromJson(jsonDecode(dataResponse.body) as Map<String, dynamic>);
final dataResponse = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
Album.fromJson(jsonDecode(dataResponse.body));
在启用 nullsafety 的项目上,Album.fromJson(jsonDecode(dataResponse.body));
此代码抛出错误 The argument type 'dynamic' can't be assigned to the parameter type 'Map<String, dynamic>'.
已关注官方doc
以下代码用于数据建模。
class Album {
final int userId;
final int id;
final String title;
Album({
required this.userId,
required this.id,
required this.title,
});
factory Album.fromJson(Map<String, dynamic> json) {
return Album(
userId: json['userId'],
id: json['id'],
title: json['title'],
);
}
}
只需要将 (Map
factory Album.fromJson(dynamic json) {
return Album(
userId: json['userId'],
id: json['id'],
title: json['title'],
);
}
模型已经不错了...
试试这个。
final dataResponse = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
List<dynamic> list =jsonDecode(dataResponse.body);
return list.map((e)=>Album.fromJson(e)).toList();
你只需要投射你的jsonDecode
Album.fromJson(jsonDecode(dataResponse.body));
显式类型:
Album.fromJson(jsonDecode(dataResponse.body) as Map<String, dynamic>);