【发布时间】:2021-12-14 16:29:18
【问题描述】:
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'],
);
}
}
【问题讨论】:
-
fromJson(Map<String, dynamic> json)改为动态fromJson(dynamic json) -
你的功能在我这边运行正常。
-
您是否尝试过启用空安全的项目?