我是颤振开发的新手。最近听说了api调用的Dio和Http包。哪个是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);
    }
  }
}

dio 和 http 是最好的。现在,我是 dio 用于

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


Dio 通常被认为比 http 更快、更高效,因为它使用专用的 HTTP 客户端并支持异步请求。这两个软件包都有活跃和支持的社区,但 Dio 通常更受欢迎,并且有更多的贡献者和可用资源。我通常更喜欢将 dio 用于 api。


在 Flutter 中,有多种调用 API 的方法,具体取决于应用程序的特定用例和要求。在 Flutter 中进行 API 调用的三种常见方式:

HTTP: http 包是一个流行的包,它提供了一种在 Flutter 中发出 HTTP 请求的简单方法。您可以使用此包进行 GET、POST、PUT 和 DELETE 请求。使用 http 包的 GET 请求示例:

import 'package:http/http.dart' as http;
final response = await 
http.get(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
if (response.statusCode == 200) {
  // Do something with the response data
} else {
 // Handle error
}

DIO: dio 包是另一个流行的包,用于在 Flutter 中发出 HTTP 请求。它提供了请求取消、文件上传、请求拦截等功能。使用dio包发起GET请求的例子:

import 'package:dio/dio.dart';
final dio = Dio();
final response = await 
dio.get('https://jsonplaceholder.typicode.com/posts');
if (response.statusCode == 200) {
   // Do something with the response data
} else {
  // Handle error
}

状态管理:如果您使用的是 Provider 或 Bloc 等状态管理库,则可以使用该库进行 API 调用。这种方法可以帮助您将 API 逻辑与 UI 逻辑分离,并使您更容易测试代码。使用 Provider 发出 GET 请求的示例:

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:http/http.dart' as http;

final postsProvider = FutureProvider<List<Post>>((ref) async {
final response = await 
http.get(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
 if (response.statusCode == 200) {
  final List<dynamic> json = jsonDecode(response.body);
  return json.map((post) => Post.fromJson(post)).toList();
 } else {
    throw Exception('Failed to load posts');
 }
});

但我建议你使用 dio。因为它简单易用,并且提供了请求取消、文件上传、请求拦截等功能


两者都是流行的软件包,但在我个人看来,dio 是最好的,因为它可以更好地控制调用 api。

你有 dio 的示例吗?

https://github.com/search?q=dio+example