首页 > 解决方案 > 在 Flutter/Dart 上创建 Chopper 客户端的最佳方式是什么?

问题描述

我在 Flutter 上使用 Chopper 进行 http 请求,据我所知,有两种方法可以创建 Chopper 客户端。一种是将服务添加到其数组中,然后将其公开:

// my_chopper_client.dart

  static ChopperClient getClient() {
    final chopper = ChopperClient(
      baseUrl: "https://example.com/",
      services: [
        ServiceOne.create(),
        ServiceTwo.create(),
        ServiceThree.create()
      ],
      converter: JsonConverter()
    );

    return chopper;
  }

// Some other file.dart

    void login() {
      ChopperClient client = MyChopperClient.getClient();
      var serviceTwo = client.getService<ServiceTwo>();
      var resp = await serviceTwo({"email": "example@mail.io", "password": "123"});
      print(resp.body);
    }

另一个是为每个服务创建一个 Chopper 客户端的新实例:

// service_two.dart

  static ServiceTwo create() {
    final client = ChopperClient(
      services: [
        _$ServiceTwo(),
      ],
      converter: JsonConverter(),
    );
    
    return _$ServiceTwo(client);
  }

// some other file.dart

  void login() async {
    var serviceTwo = ServiceTwo.create();
    var resp = await serviceTwo.login({"email": "example@mail.io", "password": "123"});
    print(resp.body);
  }

我的问题是:哪种方式可以获得最佳性能?在我看来,数组方法可能会使用更多内存。提前致谢!

标签: flutterdartchopper

解决方案


推荐阅读