Generate Model
Generate a data model with flexible JSON serialization options:
# Generate a model with interactive mode
flutter_bunny generate model --name User
# Generate a model with specific serialization
flutter_bunny generate model --name Product --serializer json_serializable
# Generate a model from JSON sample
flutter_bunny generate model --name User --json '{"id": 1, "name": "John", "email": "john@example.com"}'
# Generate a model in non-interactive mode
flutter_bunny generate model --name Order --interactive false
Options
--name
or-n
(required in non-interactive mode): The name of the model (e.g., User, Product)--directory
or-d
: Directory to create the model in (default:lib/models
)--serializer
or-s
: JSON serialization method to use:freezed
- Code generation for immutable classes with unions/pattern-matchingjson_serializable
- Simple JSON serialization (default)manual
- Custom toJson/fromJson methodsequatable
- Simple classes with value equality
--interactive
or-i
: Use interactive mode to configure model (default: true)--json
: JSON sample to generate model from (can be a string or loaded from a file)
Interactive Mode
When running in interactive mode (--interactive true
), the command will:
- Prompt for a model name if not provided
- Allow selection of serialization method with descriptions
- Offer to provide a JSON sample:
- Enter JSON directly
- Load JSON from a file
- Detect complex nested structures and offer to generate multiple related model files
- Suggest required dependencies for the chosen serialization method
Examples
Generate a User Model with Interactive Configuration
flutter_bunny generate model
This will start an interactive session to configure your model.
Generate a Product Model with Freezed Serialization
flutter_bunny generate model --name Product --serializer freezed
This will generate a Product model using Freezed for immutability and JSON serialization.
Generate a Model from a JSON File
flutter_bunny generate model --name ApiResponse --json path/to/sample.json
This will parse the JSON file and generate an appropriate model with detected fields.
Generate Multiple Related Models from Complex JSON
flutter_bunny generate model --name Order --json '{"id": 123, "items": [{"productId": 1, "quantity": 2}], "customer": {"id": 456, "name": "John Doe"}}'
In interactive mode, this will detect nested structures and offer to generate separate models for Order, OrderItem, and Customer.
Generated Output
The command will:
- Create model files in the specified directory (default:
lib/models
) - Generate appropriate serialization code based on the selected method
- Add any necessary part declarations for code generation
- Suggest required dependencies to add to your pubspec.yaml
- Provide the command to run build_runner if needed
For complex JSON structures with nested objects, multiple interrelated model files may be generated with proper imports.