-
Notifications
You must be signed in to change notification settings - Fork 19
Code Generation
If you're new to Flutter (or coming from other frameworks), you might be surprised to see files like user.g.dart or app_database.g.dart. These are generated files.
We use code generation to avoid writing boilerplate code for things like JSON serialization, database access, and localization.
This is the command that powers most of our code generation.
If you pulled the latest changes and the app isn't compiling because of missing .g.dart files, run this:
# In packages/uni_app
flutter pub run build_runner build --delete-conflicting-outputs(The --delete-conflicting-outputs flag is a lifesaver. It tells the builder to overwrite existing generated files if they conflict with the new ones.)
If you are actively modifying models or database entities, run this in a separate terminal:
flutter pub run build_runner watchThis will watch your files and automatically regenerate the necessary code whenever you hit save.
ObjectBox (Database)
We use ObjectBox for our local database.
-
Source: Your entity classes (e.g.,
lib/model/entities/exam.dart). -
Generated:
lib/objectbox.g.dart. -
Trigger: Changes to any class annotated with
@Entity.
JSON Serialization
We use json_serializable to convert Dart objects to and from JSON (for APIs).
-
Source: Your model classes.
-
Generated:
*.g.dartfiles next to your models. -
Trigger: Changes to classes annotated with
@JsonSerializable.
Translations work a bit differently. They don't use build_runner in the same way.
Source Files:
-
packages/uni_app/lib/l10n/intl_en.arb(English) -
packages/uni_app/lib/l10n/intl_pt_PT.arb(Portuguese)
How to Update:
-
Add your keys and values to the
.arbfiles. -
Run the generation command:
dart pub global activate intl_utils 2.1.0
dart pub global run intl_utils:generate(Note: VS Code with the Flutter extension will often do this automatically when you save an .arb file!)
Usage:
import 'package:uni/generated/l10n.dart';
// Inside a widget build method
Text(S.of(context).myNewStringKey);