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
Generate model files
Usage: gexd make model [<name>] [options]
Arguments:
<name> Model name (e.g., User, Profile)
[Optional: Run without arguments for interactive mode]
Options:
-h, --help Print this usage information.
-f, --file=<value> Path to JSON file for model generation
-u, --url=<value> URL to fetch JSON data for model generation
-t, --template=<basic|custom> Choose starter template for model generation
[basic] Basic model template
[custom] Custom interactive template
-s, --style=<plain|json|freezed> Choose model generation style
[plain] Simple Dart classes
[json] JSON serializable models
[freezed] Immutable data classes with Freezed
-r, --relationships-in-folder Generate relationships in separate <model>_relationships folder
-i, --immutable Generate immutable model with final fields
-c, --copyWith Add copyWith method for creating modified copies
-e, --equatable Use Equatable package for value equality comparison
--on=<path> Specify subdirectory path (max 3 levels)
--force Force overwrite existing files without prompting
Examples:
gexd make model # Interactive mode
gexd make model User # Smart mode (interactive if exists)
# Model from template (default):
gexd make model User # Basic template
gexd make model User --template custom # Custom interactive template
# Model from JSON file:
gexd make model User --file assets/models/user.json # From local file
# Model from API endpoint:
gexd make model User --url https://api.example.com/user/123
# Model with advanced features:
gexd make model User --immutable --copyWith --equatable
# Model with different styles:
gexd make model User --style json # JSON serializable
gexd make model User --style freezed --immutable # Freezed style
# Model with relationship management:
gexd make model User --file assets/models/user.json --relationships-in-folder
# Model in subdirectory:
gexd make model User --on auth/models # Model in subdirectoryπ― Real-World Examples
π Example JSON Input
Create a file assets/models/user.json:
{
"id": 1,
"name": "Ahmed Ali",
"email": "ahmed@example.com",
"age": 28,
"isActive": true,
"avatar": "https://example.com/avatar.jpg",
"profile": {
"bio": "Flutter Developer",
"website": "https://ahmed.dev",
"social": {
"twitter": "@ahmed_dev",
"github": "ahmed-dev"
}
},
"preferences": {
"theme": "dark",
"language": "ar",
"notifications": {
"email": true,
"push": false,
"sms": true
}
},
"createdAt": "2025-01-15T10:30:00Z",
"updatedAt": "2025-01-20T15:45:30Z"
}ποΈ Basic Model Generation
gexd make model User --file assets/models/user.jsonGenerated Files:
lib/app/data/models/
βββ user.dart # Main User model
βββ user_relationships/ # Nested relationships
β βββ user_profile.dart # Profile nested object
β βββ user_social.dart # Social media links
β βββ user_preferences.dart # User preferences
β βββ user_notifications.dart # Notification settingsπ¨ Advanced Model with All Features
gexd make model User \
--file assets/models/user.json \
--style freezed \
--immutable \
--copyWith \
--equatable \
--relationships-in-folder \
--on auth/modelsGenerated Code Example:
// lib/app/modules/auth/models/user.dart
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:equatable/equatable.dart';
import 'user_relationships/user_profile.dart';
import 'user_relationships/user_preferences.dart';
part 'user.freezed.dart';
part 'user.g.dart';
@freezed
class User with _$User, EquatableMixin {
const factory User({
required int id,
required String name,
required String email,
required int age,
required bool isActive,
required String avatar,
required UserProfile profile,
required UserPreferences preferences,
required DateTime createdAt,
required DateTime updatedAt,
}) = _User;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
@override
List<Object?> get props => [
id, name, email, age, isActive,
avatar, profile, preferences, createdAt, updatedAt
];
}π API-Based Model Generation
# Generate from live API endpoint
gexd make model Product --url https://api.store.com/products/1 --style json
# Generate with custom organization
gexd make model Order \
--url https://api.store.com/orders/123 \
--on ecommerce/data \
--immutable \
--copyWithβοΈ Options
--file (-f)
--file (-f)Description: Path to JSON file for model generation
Usage:
gexd make model User --file assets/models/user.json
gexd make model Product --file assets/models/product.jsonBenefits:
Local file processing
Offline development
Version controlled schemas
--url (-u)
--url (-u)Description: URL to fetch JSON data for model generation
Usage:
gexd make model User --url https://api.example.com/user/123
gexd make model Post --url https://jsonplaceholder.typicode.com/posts/1Benefits:
Live API integration
Real-time schema updates
Production data structures
--template (-t)
--template (-t)Description: Choose starter template for model generation
Format: basic|custom
Available Options:
basicβ Simple model generation with minimal configurationcustomβ Interactive template with guided options
Usage:
gexd make model User --template basic # Quick generation
gexd make model User --template custom # Interactive setup--style (-s)
--style (-s)Description: Choose model generation style
Format: plain|json|freezed
Available Options:
plainβ Simple Dart classesjsonβ JSON serializable models with toJson/fromJsonfreezedβ Immutable data classes with code generation
Usage:
gexd make model User --style plain # Basic Dart class
gexd make model User --style json # With JSON serialization
gexd make model User --style freezed # Immutable with Freezed--on
--onDescription: Specify subdirectory path for model placement
Format: path/to/directory (max 3 levels)
Usage:
gexd make model User --on auth/models # auth/models/user.dart
gexd make model Product --on shop/data/models # shop/data/models/product.dart
gexd make model Order --on ecommerce # ecommerce/order.dartπ© Flags
--relationships-in-folder (-r)
--relationships-in-folder (-r)Description: Generate nested objects in separate organized folder structure
Default: true
Usage:
# Generates nested relationships in separate folder
gexd make model User --file user.json --relationships-in-folder
# Generates everything in single file
gexd make model User --file user.json --no-relationships-in-folderOutput Structure:
lib/app/data/models/
βββ user.dart # Main model
βββ user_relationships/ # Nested models
βββ user_profile.dart
βββ user_social.dart
βββ user_preferences.dartBenefits:
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)
--immutable (-i)Description: Generate immutable model with final fields
Usage:
gexd make model User --immutableGenerated Code:
class User {
const User({
required this.id,
required this.name,
required this.email,
});
final int id;
final String name;
final String email;
}--copyWith (-c)
--copyWith (-c)Description: Add copyWith method for creating modified copies
Usage:
gexd make model User --copyWith --immutableGenerated Code:
User copyWith({
int? id,
String? name,
String? email,
}) {
return User(
id: id ?? this.id,
name: name ?? this.name,
email: email ?? this.email,
);
}--equatable (-e)
--equatable (-e)Description: Use Equatable package for value equality comparison
Usage:
gexd make model User --equatableGenerated Code:
import 'package:equatable/equatable.dart';
class User extends Equatable {
const User({required this.id, required this.name});
final int id;
final String name;
@override
List<Object?> get props => [id, name];
}--force
--forceDescription: Force overwrite existing files without prompting
Usage:
gexd make model User --force # Overwrites without askingπ― Common Workflows
π Quick API Integration
# 1. Generate from API
gexd make model User --url https://api.example.com/user/1
# 2. Add features
gexd make model User --url https://api.example.com/user/1 \
--immutable --copyWith --equatable --forceποΈ Complex Model with Relationships
# Generate user model with nested relationships
gexd make model User \
--file assets/models/user.json \
--relationships-in-folder \
--style freezed \
--immutable \
--on auth/modelsπ Iterative Development
# 1. Start simple
gexd make model Product --file assets/models/product.json
# 2. Add features iteratively
gexd make model Product --file assets/models/product.json --copyWith --force
# 3. Upgrade to Freezed
gexd make model Product --file assets/models/product.json \
--style freezed --immutable --copyWith --equatable --forceπ‘ Best Practices
π― Model Organization
Use
--onfor logical grouping: Group related models in subdirectoriesEnable
--relationships-in-folder: Keep complex models organizedUse descriptive names: Choose clear, descriptive model names
π§ Feature Selection
Start with
--style json: Good for API integrationAdd
--immutable --copyWith: For state managementUse
--equatable: For value comparison in lists/setsUpgrade to
--style freezed: For production apps
π‘οΈ Development Workflow
Test with small JSON first: Validate structure before complex models
Use
--forceduring development: Quick iterationVersion control your JSON: Track schema changes
Document your models: Add comments for complex relationships
π§ Troubleshooting
β Common Issues
Issue: Nested objects not generating properly
# β
Solution: Enable relationships folder
gexd make model User --file assets/models/user.json --relationships-in-folderIssue: Complex API responses
# β
Solution: Save API response to file first
curl "https://api.example.com/user/1" > assets/models/user.json
gexd make model User --file assets/models/user.jsonIssue: Existing files conflict
# β
Solution: Use force or choose different location
gexd make model User --file assets/models/user.json --force
# OR
gexd make model User --file assets/models/user.json --on v2/modelsGenerated automatically by gexd_doc
Last updated