中文 | English

pub License

net_retrofit_kit

Declarative HTTP client for Flutter: annotations + codegen, based on Dio. GitHub


Quick start

1. Dependencies

dependencies:
  net_retrofit_kit: ^0.2.14
  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. Accepts Map<String, dynamic> or a class model; for non-Map types the generator emits body.toJson(), so the model must implement toJson.
@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).
[CallOptions? options] Optional positional per-call options (cancelToken, clientKey). Use {CallOptions? options} when the method has other named params. See Annotations.
Future<List<T>> + T.fromJson Generator auto maps JSON arrays into models instead of direct as List<T> cast.

More: Annotations · Configuration · Multiple clients · Example


Run example

cd example && flutter pub get && dart run build_runner build --delete-conflicting-outputs && flutter run

Example project

Libraries

net_retrofit_kit
Retrofit-style networking annotations and codegen contract.