flutter中api调用哪种方式最好

kap*_* tk 5 api rest flutter dio flutter-http

我是颤振开发的新手。最近听说了Dio和Http包用于api调用。这是 api 调用最好的一个。如果有人有更好的api服务方式?

  CreateAccountRepository.internal();

  static final CreateAccountRepository _singleInstance =
      CreateAccountRepository.internal();

  factory CreateAccountRepository() => _singleInstance;

  //api: Registration
  Future<CreateAccountResponse> userRegistration(
      AccountCreateRequest requestParams) async {
    bool isNetworkAvail = await NetworkCheck().check();
    if (isNetworkAvail) {
      final response = await networkProvider.call(
          method: Method.POST,
          pathUrl: AppUrl.pathRegister,
          body: requestParams.toJson(),
          headers: headerContentTypeAndAccept);
      if (response.statusCode == HttpStatus.ok) {
        String location = response.headers['location'];
        String userId = location.split("/").last;
        CreateAccountResponse createAccountResponse =
            CreateAccountResponse.fromJson(json.decode(response.body));
        createAccountResponse.isSuccess = true;
        createAccountResponse.userId = int.parse(userId);
        return createAccountResponse;
      } else if (response.statusCode == HttpStatus.unprocessableEntity) {
        return CreateAccountResponse.fromJson(json.decode(response.body));
      } else if (response.statusCode == HttpStatus.badRequest) {
        return CreateAccountResponse.fromJson(json.decode(response.body));
      } else {
        return CreateAccountResponse.fromJson(json.decode(response.body));
      }
    } else {
      return CreateAccountResponse(
          message: AppStrings.ERROR_INTERNET_CONNECTION);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 2

dio 和 http 是最好的。\n现在,我使用 dio

\n

Dio 是 Dart 的强大 HTTP 客户端。它支持拦截器、全局配置、FormData、请求取消、文件下载和超时等。Flutter 提供了一个 http 包,\xe2\x80\x99 非常适合执行基本的网络任务,但在处理一些高级功能时却相当令人畏惧。相比之下,Dio 提供了直观的 API,可以轻松执行高级网络任务。

\n