【发布时间】:2021-08-12 14:33:16
【问题描述】:
我想将带有 hostId 值的列表数据从 Json 复制到空列表 pcBusy。我写了代码,但我得到了错误 "_TypeError (type 'int' is not a subtype of type 'List')".
感谢您的帮助!)
List pcBusy = [];
void fetchDataStandart() async {
final urlAuth =
Uri.parse('http://185.XX.XXX.XXX/api/usersessions/activeinfo');
final response = await http
.get(urlAuth, headers: <String, String>{'authorization': basicAuth});
if (response.statusCode == 200) {
List listPc = List.from(json.decode(response.body)['result']);
for (int i = 0; i < listPc.length; i++) {
pcBusy = listPc[i]['hostId'];
}
print(pcBusy);
} else {
throw Exception('Ошибка получения данных');
}
}
【问题讨论】:
-
你的返回值是int,所以不能赋值给列表,尝试打印List.from(json.decode(response.body)['result'])或者json.decode( response.body)['result'] 看看它打印了什么。