Code Generation
The project generates immutable model code, JSON serializers, Riverpod providers, localization code, and Rust bindings. Run the generator that matches the source you changed.
When to run code generation
Section titled “When to run code generation”Run code generation after changing any of these inputs:
- Models with
@freezed - Classes with
@JsonSerializable - Providers with
@riverpod - ARB localization files
- Rust APIs under
crates/sbm_ffi/src/api
Dart generation
Section titled “Dart generation”Normal generation
Section titled “Normal generation”dart run build_runner buildClean and regenerate
Section titled “Clean and regenerate”Use a clean build when the cached asset graph is out of sync with the source
tree, for example after merging generated source files. A stale
.dart_tool/build/asset_graph.json can cause build_runner to stall midway
through a phase without reporting a useful error.
dart run build_runner cleandart run build_runner buildGenerated files
Section titled “Generated files”Freezed (*.freezed.dart)
Section titled “Freezed (*.freezed.dart)”Freezed produces immutable model implementations, copyWith, equality, and
union APIs:
@freezedclass ServerState with _$ServerState { const factory ServerState.connected() = Connected; const factory ServerState.disconnected() = Disconnected; const factory ServerState.error(String message) = Error;}JSON serialization (*.g.dart)
Section titled “JSON serialization (*.g.dart)”json_serializable creates fromJson and toJson methods based on model
fields:
@JsonSerializable()class Server { final String id; final String name; final String host;
Server({required this.id, required this.name, required this.host});
factory Server.fromJson(Map<String, dynamic> json) => _$ServerFromJson(json); Map<String, dynamic> toJson() => _$ServerToJson(this);}Riverpod providers (*.g.dart)
Section titled “Riverpod providers (*.g.dart)”riverpod_generator creates provider declarations from @riverpod classes:
@riverpodclass MyNotifier extends _$MyNotifier { @override int build() => 0;}Hive readers
Section titled “Hive readers”Nothing generates a Hive adapter, and no model carries a Hive annotation. The
readers in lib/hive/ (legacy_adapters.dart, frozen_adapters.dart,
spi_legacy_adapter.dart) are hand-written and frozen: each reads the bytes a
released build wrote. Old boxes do not contain fields added since, so a reader
built from a current model can make an old box unreadable. Change one only when
data written by a released version requires it, and update its migration test
at the same time.
Rust bindings (flutter_rust_bridge)
Section titled “Rust bindings (flutter_rust_bridge)”After editing a Rust API under crates/sbm_ffi/src/api, regenerate its Dart
bindings:
flutter_rust_bridge_codegen generateThe generator reads flutter_rust_bridge.yaml and writes files to
lib/src/rust/. Do not edit those generated files. Do not run
flutter_rust_bridge_codegen integrate; its template scaffolding does not
match this repository.
Localization generation
Section titled “Localization generation”After editing a localization file in lib/l10n/*.arb, run:
flutter gen-l10nGenerated localization code is written to lib/generated/l10n/.
- build_runner 2.15 removed
--delete-conflicting-outputs; it now prints a warning and has no effect. Clear the cache as described above instead. - Include generated files in your change when this repository tracks them.
- Do not edit
*.g.dart,*.freezed.dart, or files underlib/generated/by hand. - After changing a model, complete generation before running analysis and tests.