我正在尝试从网络服务中获取一个 json 数组。
文档说:
_getIPAddress() {
final url = 'https://httpbin.org/ip';
HttpRequest.request(url).then((value) {
print(json.decode(value.responseText)['origin']);
}).catchError((error) => print(error));
}
如果我使用这段代码,我会得到错误:
The method request is not defined for the class 'HttpRequest'
如果我尝试导入:
import 'dart:html';
我收到这个错误:
Target of URI doesn't exist 'dart:html'
最佳答案
对于 http 请求,我推荐 http package .
然后在导入 http 包之后,你可以像这样使用它,例如:
import 'package:http/http.dart' as http;
_getIPAddress() async {
final url = 'https://httpbin.org/ip';
try {
http.Response res = await http.get(url);
print(json.decode(res.body));
} catch(e) {
print(e);
}
}
关于json - 从网络服务中获取 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54289420/