我正在尝试通过遵循此tutorial来填充从API获取的数据的个人资料页面。我不知道为什么,但是出现下一个错误“未处理的异常:类型'_InternalLinkedHashMap'不是强制类型转换中'列表'类型的子类型”,我从字面上查找并尝试了所有操作。 我的模型是这样的:
class Profile {
final String firstName;
final String lastName;
final String city;
final String street;
final String streetNo;
final String postcode;
final String state;
final String country;
final String phoneNumber;
final String languages;
final String certified;
Profile._({this.firstName, this.lastName, this.city, this.street, this.streetNo, this.postcode, this.state, this.country, this.phoneNumber, this.languages, this.certified});
factory Profile.fromJson(Map<String, dynamic> json) {
return Profile._(
firstName: json['response']['user']['first_name'],
lastName: json['response']['user']['last_name'],
city: json['response']['user']['city'],
street: json['response']['user']['street'],
streetNo: json['response']['user']['street_no'],
postcode: json['response']['user']['postcode'],
state: json['response']['user']['state'].toString(),
country: json['response']['user']['country'].toString(),
languages: json['response']['languages'].toString(),
certified: json['response']['certified'].toString()
);
}
}
API请求是这样的:
_fetchAndNavigate() async {
_sharedPreferences = await _prefs;
var uri = host + '/translators/' + AuthUtils.getId(_sharedPreferences);
var token =AuthUtils.getToken(_sharedPreferences);
Map<String, String> requestHeaders = {
'Authorization': 'JWT $token'
};
final response = await http.get(
uri,
headers: requestHeaders
);
if(response.statusCode == 200) {
(json.decode(response.body) as List)
.map((data) => new Profile.fromJson(data))
.toList();
print(response);
print(list);
}
}
调试器在分析“作为列表”时抛出错误,但我不明白为什么。 我不完全确定自己在做什么错或者是某种错误。非常感谢你!