我正在尝试从网络服务中获取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'
答案 0 :(得分:1)
对于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);
}
}