【发布时间】:2021-03-22 15:45:35
【问题描述】:
当我使用 ['name'] 从 json http 链接打印名称时,我收到此错误:
Unhandled exception:
type 'String' is not a subtype of type 'int' of 'index'
代码是:
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() {
print(getData());
}
Future<http.Response?> getData() {
return http
.get(Uri.parse("https://jsonplaceholder.typicode.com/users"))
.then((http.Response response) {
var a = jsonDecode(response.body)['name'];
print(a);
});
}
【问题讨论】:
-
返回的 JSON 是一个对象列表。直接在这个列表中查询“name”没有任何意义。
标签: http dart asynchronous uri future