Skip to content

Code Generation

Flutter Server Box heavily uses code generation for models, state management, and serialization.

Run after modifying:

  • Models with @freezed annotation
  • Classes with @JsonSerializable
  • Hive models
  • Providers with @riverpod
  • Localizations (ARB files)
Terminal window
# Generate all code
dart run build_runner build --delete-conflicting-outputs
# Clean and regenerate
dart run build_runner build --delete-conflicting-outputs --clean

Immutable data models with union types:

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

Generated from json_serializable:

@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);
}

Generated from @riverpod annotation:

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

Auto-generated for Hive models (hive_ce):

@HiveType(typeId: 0)
class ServerModel {
@HiveField(0)
final String id;
}
Terminal window
flutter gen-l10n

Generates lib/generated/l10n/ from lib/l10n/*.arb files.

  • Use --delete-conflicting-outputs to avoid conflicts
  • Add generated files to .gitignore
  • Never manually edit generated files