retrofit_generator
10.3.3, released 3 weeks ago by hitrevor.com
Generates Dio client code from Retrofit‑style annotations.
What it does
It reads abstract classes annotated with @RestApi and produces concrete implementations that use Dio for HTTP calls. Supports built_collection, lean_builder and custom builder options.
AI-written summary of version 10.3.3, based on the package's README and metadata. Report an inaccuracy
Use it when
- You want type‑safe, compile‑time generated REST clients in Dart/Flutter
- You prefer Retrofit‑style declarative API definitions over manual Dio requests
Skip it when
- You need runtime‑only solutions without build_runner
- You require advanced features not covered by Retrofit annotations
Strengths
- Reduces boilerplate for Dio calls
- Integrates with build_runner and supports custom options
- Keeps API definitions clean and type‑safe
Weaknesses
- Requires code generation step (build_runner)
- Limited to features exposed by the package’s annotations
Setup
- Kind
- Pure Dart
- Effort
- Medium
- Code generation
- Yes (build_runner)
- Direct dependencies
- 9
Add retrofit_generator and retrofit to pubspec, run flutter pub get, then execute flutter pub run build_runner build to generate code.
import 'package:retrofit/retrofit.dart';
import 'package:dio/dio.dart';
part 'example.g.dart';
@RestApi(baseUrl: "https://api.example.com")
abstract class ApiClient {
factory ApiClient(Dio dio, {String baseUrl}) = _ApiClient;
@GET("/users/{id}")
Future<User> getUser(@Path("id") String id);
}
void main() async {
final dio = Dio();
final client = ApiClient(dio);
final user = await client.getUser('123');
print(user);
}Alternatives
Packages that do the same job and could replace retrofit_generator.
- chopper_generator
Generates type‑safe Chopper client code from annotated service interfaces.
78Compare - swagger_parser
Generate Dart models and HTTP clients from Swagger and OpenAPI schemas.
87Compare - tonik
Pure Dart OpenAPI 3.0/3.1 code generator for type-safe API client packages.
74Compare - openapi_generator
Generator for OpenAPI client SDKs in Dart and Flutter projects.
67Compare
Works with
Packages commonly used together with retrofit_generator: ones it builds on, ones built on it, and its own family.
- analyzerretrofit_generator depends on itPerforms static analysis of Dart code for tool integration and embedding.
- dart_styleretrofit_generator depends on itProvides an API and CLI to format Dart source code automatically.
- code_builderretrofit_generator depends on itA fluent, builder-based library for generating valid Dart code programmatically.
- built_collectionretrofit_generator depends on itImmutable collections based on SDK collections with builder pattern.
- buildretrofit_generator depends on itPackage for authoring build_runner compatible code generators.
- source_genretrofit_generator depends on itSource code generation builders and utilities for the Dart build system.
- dioretrofit_generator depends on itA powerful HTTP networking package for Dart and Flutter with interceptors and request cancellation.
- protobufretrofit_generator depends on itProvides runtime support for Dart protocol buffer messages generated by protoc_plugin.