Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振通用RESTAPI调用函数_Flutter_Dart - Fatal编程技术网

Flutter 颤振通用RESTAPI调用函数

Flutter 颤振通用RESTAPI调用函数,flutter,dart,Flutter,Dart,我正在开发我的第一个颤振应用程序。调用rest api并返回结果所需的应用程序。我希望创建generic函数来调用RESTAPI。我已经写了下面的代码,但我并没有轻描淡写,我如何才能解码特定模型中的api响应 Future<T> apiRequest<T>( String endPoint, RequestMethod method, { String body = '', String token = '', }) async {

我正在开发我的第一个颤振应用程序。调用rest api并返回结果所需的应用程序。我希望创建
generic
函数来调用RESTAPI。我已经写了下面的代码,但我并没有轻描淡写,我如何才能解码特定模型中的api响应

Future<T> apiRequest<T>(
    String endPoint,
    RequestMethod method, {
    String body = '',
    String token = '',
  }) async {
    http.Response resp;
    final String url = LocalConstants.apiBaseUrl + endPoint;
    final Map<String, String> headers = new Map<String, String>();
    headers.putIfAbsent(
        HttpHeaders.contentTypeHeader, () => 'application/json');
    if (token != null && token.isNotEmpty) {
      headers.putIfAbsent(
          HttpHeaders.authorizationHeader, () => 'Bearer ' + token);
    }
    try {
      if (method == RequestMethod.get) {
        resp = await http.get(
          url,
          headers: headers,
        );
      } else if (method == RequestMethod.put) {
        resp = await http.put(
          url,
          headers: headers,
          body: body,
        );
      } else if (method == RequestMethod.post) {
        resp = await http.post(
          url,
          headers: headers,
          body: body,
        );
      } else if (method == RequestMethod.delete) {
        resp = await http.delete(
          url,
          headers: headers,
        );
      }
      if (resp != null && this.validateResponse(resp)) {
        return json.decode(resp.body);
      }
      // else {
      //   Response resp = new Response();
      //   resp.respMsg = LocalConstants.genericError;
      //   resp.respCode = LocalConstants.resp_failure;
      //   Response.
      // }
    } on TimeoutException catch (e) {
      //handleTimeout();
    } on SocketException catch (e) {
      print('Socket Error: $e');
      //handleTimeout();
    } on Error catch (e) {
      print('General Error: $e');
      //showError();
    }
  }

如何解码body
json.decode(resp.body)
到类型为
T

GenericResp
,您可以添加一个泛型参数,将json数据反序列化到
GenericResp
。诸如此类:

Future<T> apiRequest<T>(
    String endPoint,
    RequestMethod method, T Function(Object json) fromJson, {
      String body = '',
      String token = '',
    }) async { ... }
然后一个电话会是这样的:

await ApiService.newInstance(context).apiRequest<GenericResp>('/api/people',
    RequestMethod.get, (json) => GenericResp.fromJson(json));
wait-ApiService.newInstance(context).apirest('/api/people',
RequestMethod.get,(json)=>GenericResp.fromJson(json));

无法理解您对t类型的GenericResp所说的话,GenericResp已经是一个
类了
Future<T> apiRequest<T>(
    String endPoint,
    RequestMethod method, T Function(Object json) fromJson, {
      String body = '',
      String token = '',
    }) async { ... }
if (resp != null && this.validateResponse(resp)) {
  return fromJson(json.decode(resp.body));
}
await ApiService.newInstance(context).apiRequest<GenericResp>('/api/people',
    RequestMethod.get, (json) => GenericResp.fromJson(json));