retrofit
4.10.0, released 3 months ago by hitrevor.com
Dart Dio client generator using source_gen
WASM-readyRock solid
What it does
Retrofit.dart generates typed Dio API clients from annotated interfaces. It uses source_gen to produce boilerplate, enabling compile‑time type safety and easy HTTP method mapping.
AI-written summary of version 4.10.0, based on the package's README and metadata. Report an inaccuracy
Use it when
- Need a typed API client for a REST service
- Want to avoid writing repetitive Dio request code
- Prefer a Retrofit‑style declarative API definition
- Require support for many HTTP methods and custom headers
Skip it when
- You prefer not to use code generation
- You are already using the plain http package
- You need a lightweight solution without Dio
Strengths
- Automatic code generation reduces boilerplate
- Strong type safety with Dart generics
- Supports custom headers, query params, body extras
- Extensible via CallAdapters and DartMappable parser
Weaknesses
- Requires build_runner and extra dev dependency
- Adds compile‑time overhead
- Tied to Dio, not the standard http client
Setup
- Kind
- Pure Dart
- Effort
- Medium
- Code generation
- Yes (build_runner)
- Direct dependencies
- 2
Add retrofit and retrofit_generator to dependencies/dev_dependencies, run `dart pub run build_runner build` or watch. No special permissions needed.
import 'package:dio/dio.dart';
import 'package:retrofit/retrofit.dart';
@RestApi(baseUrl: 'https://api.example.com/')
abstract class ApiService {
factory ApiService(Dio dio, {String? baseUrl}) = _ApiService;
@GET('/users')
Future<List<User>> getUsers();
}
@JsonSerializable()
class User {
const User({this.id, this.name});
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
final int? id;
final String? name;
Map<String, dynamic> toJson() => _$UserToJson(this);
}
Alternatives
Packages that do the same job and could replace retrofit.
Works with
Packages commonly used together with retrofit: ones it builds on, ones built on it, and its own family.
- dioretrofit depends on itA powerful HTTP networking package for Dart and Flutter with interceptors and request cancellation.
- retrofit_generatorBuilt on retrofitGenerates Dio client code from Retrofit‑style annotations.
- google_maps_apisBuilt on retrofitDart client library for Google Maps Web Services APIs.
- nakamaBuilt on retrofitDart and Flutter client for the open-source Nakama game server.
- flutter_better_authBuilt on retrofitDart and Flutter client for the Better Auth platform.
- better_auth_flutterBuilt on retrofitFlutter SDK for Better Auth providing email/password, social sign‑in, session management and reactive auth state.
- cloudflareBuilt on retrofitDart SDK and client for Cloudflare APIs including Images, Stream, Live Inputs, and R2 storage.
- google_visionBuilt on retrofitIntegrates Google Vision REST API features for image analysis and OCR.