我试图从服务器获取数据,看起来像这样:
[
{
"key": "value",
"key": "value",
"key": "value",
"key": "value",
"key": [
"key": "value",
"key": "value",
],
"key": "value",
"key": "value",
}
]
我的模型类有这个:更新:
List<VehicleDetails> vehicleDetailsFromJson(String str) => List<VehicleDetails>.from(json.decode(str).map((x) => VehicleDetails.fromJson(x)));
factory ModelClassName.fromJson(Map<String, dynamic> json) => ModelClassName()
我的服务类有这个:更新:
Future<List<ModelClassName>> fetchDetails() async{
final response = await get(Uri.parse('$url'),
headers: {
'Authorization': "bearer _accessToken",
'Content-Type': 'application/json'
},);
return modelClassFromJson(response.body);
}
我正在使用FutureBuilder()来获取和更新我的屏幕
FutureBuilder(
future: fetchDetails(),
builder: (_, snapshot) {
print(snapshot.error);
if(snapshot.connectionState == ConnectionState.done)
return Widget();
else
return Center(
child: CircularProgressIndicator(),
);
},
),
我得到的错误:
类型'double'不是类型'int'的子类型
我怎样才能度过这个难关?请帮助。
更改您的modelClassFromJson
函数
ModelClassName modelClassFromJson(String str) {
final jsonData = json.decode(str);
return ModelClassName.fromJson(jsonData[0]);
}