Build Command
The build
command runs the build_runner
tool to generate code for your Flutter project. This is particularly useful for generating code from annotations, such as JSON serialization, freezed models, or injectable dependencies.
Usage
flutter_bunny build [options]
Alias: flutter_bunny codegen [options]
Options
-watch
orw
: Watch for file changes and rebuild as needed-delete-conflicting-outputs
ord
: Delete conflicting outputs to resolve build conflicts (default: true)-directory
orC
: Directory in which to run the build_runner (default: ".")
Examples
Run Build Runner Once
flutter_bunny build
This will run the build_runner once to generate code from annotations.
Run Build Runner in Watch Mode
flutter_bunny build --watch
This will start the build_runner in watch mode, which will automatically regenerate code when files change.
Run Build Runner with Custom Options
flutter_bunny build --delete-conflicting-outputs --directory ./packages/my_package
This will run the build_runner in the specified directory with the option to delete conflicting outputs.
Common Use Cases
The build
command is commonly used for:
- JSON Serialization: With packages like
json_serializable
- Immutable Models: With packages like
freezed
- Dependency Injection: With packages like
injectable
- Routing: With packages like
auto_route
- State Management: With code generation features in packages like Bloc
Example Workflow
- Add annotations to your code:
// user.dart
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
@JsonSerializable()
class User {
final int id;
final String name;
final String email;
User({required this.id, required this.name, required this.email});
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
- Run the build command:
flutter_bunny build
- The generated code (e.g.,
user.g.dart
) will be created automatically.
Troubleshooting
If you encounter issues with the build command:
- Conflicting Outputs: Use the
-delete-conflicting-outputs
flag - Build Failures: Check your annotations and dependencies
- Missing Dependencies: Ensure all required packages are in your
pubspec.yaml
- Path Issues: Use the
-directory
option to specify the correct path