Skip to content

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.

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
Terminal window
dart run build_runner build

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.

Terminal window
dart run build_runner clean
dart run build_runner build

Freezed produces immutable model implementations, copyWith, equality, and union APIs:

@freezed
class ServerState with _$ServerState {
const factory ServerState.connected() = Connected;
const factory ServerState.disconnected() = Disconnected;
const factory ServerState.error(String message) = Error;
}

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_generator creates provider declarations from @riverpod classes:

@riverpod
class MyNotifier extends _$MyNotifier {
@override
int build() => 0;
}

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.

After editing a Rust API under crates/sbm_ffi/src/api, regenerate its Dart bindings:

Terminal window
flutter_rust_bridge_codegen generate

The 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.

After editing a localization file in lib/l10n/*.arb, run:

Terminal window
flutter gen-l10n

Generated 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 under lib/generated/ by hand.
  • After changing a model, complete generation before running analysis and tests.