http Dart包示例已经过时,如何使用client.get()
http Dart package examples are outdated, how to use client.get()
这里是http包给出的第二个例子:https://pub.dev/packages/http
var client = http.Client();
try {
var uriResponse = await client.post('https://example.com/whatsit/create',
body: {'name': 'doodle', 'color': 'blue'});
print(await client.get(uriResponse.bodyFields['uri']));
} finally {
client.close();
}
我收到错误:uriResponse.bodyFields['uri'] no such method.
我可以看到 class 响应中没有 属性 命名的 bodyFields:https://pub.dev/documentation/http/latest/http/Response-class.html
那么我应该如何使用client.get()
?
我检查了函数 https://pub.dev/documentation/http/latest/http/get.html 的文档,然后我写了这个:
print(await client.get(uriResponse.request.url, {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
}));
但这也失败并出现错误:
Class 'IOClient' has no instance method 'get' with matching arguments.
E/flutter (19613): Receiver: Instance of 'IOClient'
E/flutter (19613): Tried calling: get(Instance of '_SimpleUri', Instance of '_CompactLinkedHashSet<Map<String, String>>')
E/flutter (19613): Found: get(dynamic, {Map<String, String> headers}) => Future<Response>
如下所示编写您的 post
并单独打印回复。
final response = await client.post(
'*endPoint url*',
headers: *headers if you have any*,
body: jsonEncode({'name': 'doodle', 'color': 'blue'}
));
print(json.decode((response.body)));
get
方法也遵循相同的结构。
String url = "url";
Response response = await client.get(url);
print('Response status: ${response.statusCode}');
print('Response body: ${json.decode((response.body))}');
这里是http包给出的第二个例子:https://pub.dev/packages/http
var client = http.Client();
try {
var uriResponse = await client.post('https://example.com/whatsit/create',
body: {'name': 'doodle', 'color': 'blue'});
print(await client.get(uriResponse.bodyFields['uri']));
} finally {
client.close();
}
我收到错误:uriResponse.bodyFields['uri'] no such method.
我可以看到 class 响应中没有 属性 命名的 bodyFields:https://pub.dev/documentation/http/latest/http/Response-class.html
那么我应该如何使用client.get()
?
我检查了函数 https://pub.dev/documentation/http/latest/http/get.html 的文档,然后我写了这个:
print(await client.get(uriResponse.request.url, {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
}));
但这也失败并出现错误:
Class 'IOClient' has no instance method 'get' with matching arguments.
E/flutter (19613): Receiver: Instance of 'IOClient'
E/flutter (19613): Tried calling: get(Instance of '_SimpleUri', Instance of '_CompactLinkedHashSet<Map<String, String>>')
E/flutter (19613): Found: get(dynamic, {Map<String, String> headers}) => Future<Response>
如下所示编写您的 post
并单独打印回复。
final response = await client.post(
'*endPoint url*',
headers: *headers if you have any*,
body: jsonEncode({'name': 'doodle', 'color': 'blue'}
));
print(json.decode((response.body)));
get
方法也遵循相同的结构。
String url = "url";
Response response = await client.get(url);
print('Response status: ${response.statusCode}');
print('Response body: ${json.decode((response.body))}');