2 回答
TA贡献1886条经验 获得超2个赞
附加信息
有一个 dart 包为 http 请求提供了一些帮助类。
Github:https : //github.com/Ephenodrom/Dart-Basic-Utils
安装它:
dependencies:
basic_utils: ^1.5.1
它也是 EZ-Flutter Collection 的一部分:
Github:https : //github.com/Ephenodrom/EZ-Flutter 文档:https : //ez-flutter.de/docs
dependencies:
ez_flutter: ^0.2.5
用法
Map<String, String> headers = {
"Some": "Header"
};
Map<String, String> queryParameters = {
"Some": "Parameter"
};
String url = "";
Map payload = "{}";
Map<String, dynamic> reaponseBody;
try {
responseBody = await HttpUtils.postForJson(url, json. encode(payload) ,
queryParameters: queryParameters, headers: headers);
} catch (e) {
// Handle exception, for example if response status code != 200-299
}
// do something with the response body
附加信息 :
这些都是来自 HttpUtils 类的方法。
Future<Map<Response> getForFullResponse(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> getForJson(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<String> getForString(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<Response> postForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> postForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> postForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response> putForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> putForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> putForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response deleteForFullResponse(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> deleteForJson(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> deleteForString(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Map<String, dynamic> getQueryParameterFromUrl(String url);
String addQueryParameterToUrl(String url, Map<String, dynamic> queryParameters);
TA贡献1876条经验 获得超7个赞
我可以在 Dart 代码中看到您正在尝试直接发送 Map 对象,而不是首先将其转换为例如 JSON。
要转换为 JSON,您可以使用 dart:convert 包和以下方法:
var encodedLoginObj = json.encode(loginObj);
- 2 回答
- 0 关注
- 184 浏览
添加回答
举报