有一个 dart 包,它为 http 请求提供了一些帮助类。
Github:https ://github.com/Ephenodrom/Dart-Basic-Utils
安装它:
dependencies:
basic_utils: ^1.5.1
它也是 EZ-Flutter 系列的一部分:
Github:https ://github.com/Ephenodrom/EZ-Flutter
文档:https ://ez-flutter.de/docs
dependencies:
ez_flutter: ^0.2.5
在您的情况下,您不能在 GET 请求中发送正文。您必须在您调用的 url 中设置 id。用法
Map<String, String> headers = {
"Some": "Header"
};
Map<String, String> queryParameters = {
"Some": "Parameter"
};
String url = "example.com/api/v1/something/;
String id ="100"
url = url + id;
Map<String, dynamic> reaponseBody;
try {
responseBody = await HttpUtils.getForJson(url,
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);