Model


πŸ“ Description

Generate type-safe Dart model files from JSON sources with advanced features like nested relationships, serialization, immutability, and complex data structures. Perfect for API integration and data modeling.


βš™οΈ Usage

gexd make model [<name>] [options]

πŸš€ Key Features

🎯 Multiple Data Sources

  • JSON Files: Generate from local JSON files

  • API Endpoints: Fetch and generate from live APIs

  • Interactive Templates: Guided model creation

πŸ—οΈ Advanced Architecture Support

  • GetX Templates: Modular feature-based architecture

  • Clean Architecture: Domain-driven design with layered structure

  • Custom Organization: Flexible subdirectory placement

πŸ”§ Model Features

  • Type Safety: Automatic type detection and conversion

  • Nested Objects: Complex relationship handling

  • Immutable Models: Thread-safe data structures

  • Serialization: JSON to/from Dart conversion

  • Equatable Integration: Value equality comparison

πŸ“ Relationship Management

  • Automatic Detection: Smart identification of nested objects

  • Separate Folder Generation: Organized relationship files

  • Dependency Resolution: Proper import management


πŸ“– Detailed Usage


🎯 Real-World Examples

πŸ“„ Example JSON Input

Create a file assets/models/user.json:

πŸ—οΈ Basic Model Generation

Generated Files:

🎨 Advanced Model with All Features

Generated Code Example:

πŸ”— API-Based Model Generation


βš™οΈ Options

--file (-f)

Description: Path to JSON file for model generation

Usage:

Benefits:

  • Local file processing

  • Offline development

  • Version controlled schemas


--url (-u)

Description: URL to fetch JSON data for model generation

Usage:

Benefits:

  • Live API integration

  • Real-time schema updates

  • Production data structures


--template (-t)

Description: Choose starter template for model generation

Format: basic|custom

Available Options:

  • basic β†’ Simple model generation with minimal configuration

  • custom β†’ Interactive template with guided options

Usage:


--style (-s)

Description: Choose model generation style

Format: plain|json|freezed

Available Options:

  • plain β†’ Simple Dart classes

  • json β†’ JSON serializable models with toJson/fromJson

  • freezed β†’ Immutable data classes with code generation

Usage:


--on

Description: Specify subdirectory path for model placement

Format: path/to/directory (max 3 levels)

Usage:


🚩 Flags

--relationships-in-folder (-r)

Description: Generate nested objects in separate organized folder structure

Default: true

Usage:

Output Structure:

Benefits:

  • Clean Organization: Separate files for each nested object

  • Better Maintainability: Easy to locate and modify specific models

  • Import Management: Automatic import handling

  • Scalability: Handles complex nested structures gracefully


--immutable (-i)

Description: Generate immutable model with final fields

Usage:

Generated Code:


--copyWith (-c)

Description: Add copyWith method for creating modified copies

Usage:

Generated Code:


--equatable (-e)

Description: Use Equatable package for value equality comparison

Usage:

Generated Code:


--force

Description: Force overwrite existing files without prompting

Usage:


🎯 Common Workflows

πŸš€ Quick API Integration

πŸ—οΈ Complex Model with Relationships

πŸ”„ Iterative Development


πŸ’‘ Best Practices

🎯 Model Organization

  1. Use --on for logical grouping: Group related models in subdirectories

  2. Enable --relationships-in-folder: Keep complex models organized

  3. Use descriptive names: Choose clear, descriptive model names

πŸ”§ Feature Selection

  1. Start with --style json: Good for API integration

  2. Add --immutable --copyWith: For state management

  3. Use --equatable: For value comparison in lists/sets

  4. Upgrade to --style freezed: For production apps

πŸ›‘οΈ Development Workflow

  1. Test with small JSON first: Validate structure before complex models

  2. Use --force during development: Quick iteration

  3. Version control your JSON: Track schema changes

  4. Document your models: Add comments for complex relationships


πŸ”§ Troubleshooting

❌ Common Issues

Issue: Nested objects not generating properly

Issue: Complex API responses

Issue: Existing files conflict


Generated automatically by gexd_doc

Last updated