Cast Complex JSON 飞镖
Cast Complex JSON Dart
下面的 dart 代码允许您解码来自 resultValue 后端的 json,当我去创建具有以下值的列表我有以下错误,原因是什么?我该如何解决?
错误:
//Line: return data.map((jsonObject) => new Manutenzione(
flutter: Errore: type 'List<String>'
is not a subtype of type 'String' in type cast
飞镖代码:
var urlmod = await Storage.leggi("URL") + "/services/rest/v3/processes/PreventiveMaint/instances";
final resultValue = await apiRequest(Uri.parse(urlmod), {}, UrlRequest.GET, true);
Map<String, dynamic> map = json.decode(resultValue);
List<dynamic> data = map["data"];
try {
return data
.map((jsonObject) => new Manutenzione(
["_id"] as String,
["_user"] as String,
["__user_description"] as String,
["Description"] as String,
["ShortDescr"] as String,
["_Site_description"] as String,
["_Team_description"] as String,
["_status_description"] as String,
["_Company_description"] as String,
))
.toList();
} catch (err) {
print("Errore: " + err.toString());
}
return List.empty();
}
JSON:
{
...
data: [
{
_id: value1,
_user: value2,
..
},
{
_id: value1,
_user: value2,
..
},
]
}
您忘记在方括号前指定 jsonObject
。
var urlmod = await Storage.leggi("URL") + "/services/rest/v3/processes/PreventiveMaint/instances";
final resultValue = await apiRequest(Uri.parse(urlmod), {}, UrlRequest.GET, true);
Map<String, dynamic> map = json.decode(resultValue);
List<dynamic> data = map["data"];
try {
return data
.map((jsonObject) => new Manutenzione(
jsonObject["_id"] as String,
jsonObject["_user"] as String,
jsonObject["__user_description"] as String,
jsonObject["Description"] as String,
jsonObject["ShortDescr"] as String,
jsonObject["_Site_description"] as String,
jsonObject["_Team_description"] as String,
jsonObject["_status_description"] as String,
jsonObject["_Company_description"] as String,
))
.toList();
} catch (err) {
print("Errore: " + err.toString());
}
return List.empty();
}
下面的 dart 代码允许您解码来自 resultValue 后端的 json,当我去创建具有以下值的列表我有以下错误,原因是什么?我该如何解决?
错误:
//Line: return data.map((jsonObject) => new Manutenzione(
flutter: Errore: type 'List<String>'
is not a subtype of type 'String' in type cast
飞镖代码:
var urlmod = await Storage.leggi("URL") + "/services/rest/v3/processes/PreventiveMaint/instances";
final resultValue = await apiRequest(Uri.parse(urlmod), {}, UrlRequest.GET, true);
Map<String, dynamic> map = json.decode(resultValue);
List<dynamic> data = map["data"];
try {
return data
.map((jsonObject) => new Manutenzione(
["_id"] as String,
["_user"] as String,
["__user_description"] as String,
["Description"] as String,
["ShortDescr"] as String,
["_Site_description"] as String,
["_Team_description"] as String,
["_status_description"] as String,
["_Company_description"] as String,
))
.toList();
} catch (err) {
print("Errore: " + err.toString());
}
return List.empty();
}
JSON:
{
...
data: [
{
_id: value1,
_user: value2,
..
},
{
_id: value1,
_user: value2,
..
},
]
}
您忘记在方括号前指定 jsonObject
。
var urlmod = await Storage.leggi("URL") + "/services/rest/v3/processes/PreventiveMaint/instances";
final resultValue = await apiRequest(Uri.parse(urlmod), {}, UrlRequest.GET, true);
Map<String, dynamic> map = json.decode(resultValue);
List<dynamic> data = map["data"];
try {
return data
.map((jsonObject) => new Manutenzione(
jsonObject["_id"] as String,
jsonObject["_user"] as String,
jsonObject["__user_description"] as String,
jsonObject["Description"] as String,
jsonObject["ShortDescr"] as String,
jsonObject["_Site_description"] as String,
jsonObject["_Team_description"] as String,
jsonObject["_status_description"] as String,
jsonObject["_Company_description"] as String,
))
.toList();
} catch (err) {
print("Errore: " + err.toString());
}
return List.empty();
}