【发布时间】:2020-03-24 20:49:05
【问题描述】:
所以我在发出 post 请求时遇到了 Flutter http 包的问题,所以我使用了 dart HttpClient。我根据某处描述的内容提出了发布请求,但在获得回复时遇到问题。这是我的代码
Future<HttpClientResponse> submit() async {
print('start');
Map<String, dynamic> data = { 'title' : 'My first post' };
String jsonString = json.encode(data); // encode map to json
String paramName = 'param'; // give the post param a name
String formBody = paramName + '=' + Uri.encodeQueryComponent(jsonString);
List<int> bodyBytes = utf8.encode(formBody); // utf8 encode
HttpClientRequest request =
await HttpClient().postUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
// it's polite to send the body length to the server
request.headers.set('Content-Length', bodyBytes.length.toString());
request.headers.set('Content-Type', 'application/json');
request.add(bodyBytes);
print('done');
return await (request.close());
}
我如何得到这个请求的响应?
【问题讨论】:
-
submit().then((response) {...}) -
@pskink 提交方法进入哪里?
-
你想什么时候使用它?在某个按钮的点击监听器上?
-
@pskink 我在颤振应用程序中使用它。我有一个按钮调用提交方法
-
所以我不明白你之前的问题:
"where does the submit method go into"如果你已经在监听器中调用了submit()方法