net_retrofit_kit 0.2.1
net_retrofit_kit: ^0.2.1 copied to clipboard
基于 Dio 的声明式 HTTP 客户端:用注解定义 API 接口并自动生成实现,少写样板代码,支持多 Client、自定义响应解析与统一错误处理,便于接入现有项目。
中文 | English
net_retrofit_kit #
Declarative HTTP client for Flutter: annotations + codegen, based on Dio. GitHub
Quick start #
1. Dependencies
dependencies:
net_retrofit_kit: ^0.1.0
dio: ">=5.0.0"
dev_dependencies:
build_runner: ^2.4.0
2. Config (once, e.g. in main())
NetRequest.options = const NetOptions(
baseUrl: 'https://api.example.com',
connectTimeout: Duration(seconds: 30),
receiveTimeout: Duration(seconds: 30),
);
3. Define API
@NetApi()
abstract class UserApi {
static UserApi get instance => UserApiImpl();
@Get('/user/info')
Future<UserModel?> getUserInfo();
@Post('/login')
Future<AuthModel?> login(@Body() LoginRequest body);
}
4. Generate
dart run build_runner build --delete-conflicting-outputs
5. Use
await UserApi.instance.getUserInfo();
Quick reference (annotations) #
| Annotation | Use |
|---|---|
@NetApi() |
On abstract class. Optional client: 'upload' for named client; else uses NetRequest.defaultKey. |
@Get(path) @Post(path) @Put(path) @Delete(path) |
HTTP method + path. |
@Body() |
Request body. |
@Query() |
Full query map. @QueryKey('name') = single query param. |
@Path('id') |
Path param for :id in path. |
@Header('Authorization') |
Request header. |
@DataPath('key') |
Parse from response.data['key']. |
@Part('file') |
Multipart part (with ContentType.formData). |
@StreamResponse() |
Return stream (SSE / line stream). |
More: Annotations · Configuration · Multiple clients · Example
Run example #
cd example && flutter pub get && dart run build_runner build --delete-conflicting-outputs && flutter run