使用fromJson
时
无法将参数类型“String”分配给参数类型“Map〈String,dynamic〉”。
final fetchUserProvider = FutureProvider((ref) {
const url = 'https://jsonplaceholder.typicode.com/users/1';
return http
.get(Uri.parse(url))
.then((response) => UserModel.fromJson(response.body));//error
});
class UserModel {
final int id;
final String name;
final String username;
const UserModel({
required this.id,
required this.name,
required this.username,
});
static UserModel fromJson(Map<String, dynamic> json) {
return UserModel(
id: json['id'],
name: json['name'],
username: json['username'],
);
}
@override
String toString() => "$id, $name, $username";
}
1条答案
按热度按时间j9per5c41#
您需要使用
jsonDecode
(Fromdart:convert;
)解码响应正文,而从JSON解码,MapfromJson(Map<String, dynamic> json)
除外