flutter - 在 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);
}
我的问题是:哪种方式可以获得最佳性能?在我看来,数组方法可能会使用更多内存。提前致谢!
解决方案
推荐阅读
- latex - 无法编译乳胶文件
- css - 比 html 标签大的是什么?
- react-native - 如何从 fetch API 获取 JSON 数组的嵌套元素?
- html - 如何将 flexbox 的容器置于网格顶部的中心?
- ios - Swift iOS 项目在创建空项目后立即泄漏
- ios - 如何在使用 AVPlayer 播放视频之前检测 AirPlay 是否处于活动状态?
- javascript - 如何使用 onkeypress 捕获可变数据?
- python-3.x - 使用networkx模块在python中绘制图形时如何给出x和y标签?
- c++ - 删除 *p 是删除 [] p 的替代方法吗?
- android - Firemonkey:当 VKAutoShowMode 为 Never 时弹出软键盘