chopper_generator
8.7.1, released 7 weeks ago by hadrienlejard.io
Generates type‑safe Chopper client code from annotated service interfaces.
What it does
It reads Dart files with @ChopperApi annotations and produces .chopper.dart files containing request methods, serializers, and error handling. The generated code works with the Chopper runtime to perform HTTP calls.
AI-written summary of version 8.7.1, based on the package's README and metadata. Report an inaccuracy
Use it when
- You want compile‑time generated REST client code with minimal boilerplate
- You already use the Chopper package for networking
- You prefer strongly typed API interfaces over manual request building
Skip it when
- You need a runtime‑only solution without build_runner
- You prefer a different HTTP client API such as Dio or Retrofit
Strengths
- Reduces boilerplate by generating request code
- Integrates tightly with Chopper’s interceptor and serialization system
- Supports advanced features like query parameters and abortable requests
Weaknesses
- Requires build_runner and source_gen setup
- Only works with Chopper; not a standalone HTTP client
- Generated code can be harder to debug than hand‑written requests
Setup
- Kind
- Pure Dart
- Effort
- Medium
- Code generation
- Yes (build_runner)
- Direct dependencies
- 9
Add dev:chopper_generator and dev:build_runner to dev_dependencies, then run `dart run build_runner build --delete-conflicting-outputs`.
import 'package:chopper/chopper.dart';
@ChopperApi()
abstract class MyService extends ChopperService {
@Get(path: '/users')
Future<Response> getUsers();
}
void main() async {
final chopper = ChopperClient(services: [MyService.create()],);
final service = chopper.getService<MyService>();
final response = await service.getUsers();
print(response.body);
}Alternatives
Packages that do the same job and could replace chopper_generator.
Works with
Packages commonly used together with chopper_generator: ones it builds on, ones built on it, and its own family.
- analyzerchopper_generator depends on itPerforms static analysis of Dart code for tool integration and embedding.
- code_builderchopper_generator depends on itA fluent, builder-based library for generating valid Dart code programmatically.
- loggingchopper_generator depends on itProvides configurable logging with levels and stream listeners for Dart apps.
- built_collectionchopper_generator depends on itImmutable collections based on SDK collections with builder pattern.
- buildchopper_generator depends on itPackage for authoring build_runner compatible code generators.
- source_genchopper_generator depends on itSource code generation builders and utilities for the Dart build system.
- chopperchopper_generator depends on itAn HTTP client generator for Dart and Flutter using source_gen.
- build_runnerUsed togetherA build system for Dart code generation and modular compilation.