model_from_json 1.0.0
model_from_json: ^1.0.0 copied to clipboard
A Dart CLI tool that generates Equatable-ready Dart model classes directly from JSON files, with support for nested objects, lists, and automatic fromJson/toJson methods.
Model From JSON ๐ #
A lightweight Dart CLI tool that generates Equatable-ready Dart model classes directly from a JSON file.
Designed for Flutter developers who want:
โ
Fast model generation
โ
Nested model support
โ
List of nested objects
โ
fromJson() + toJson() built-in
โ
Clean multi-file output
โ
Interactive CLI mode
โจ Features #
- โ Generates Dart model classes automatically from JSON
- โ
Supports nested objects (
profile,subscription, etc.) - โ
Supports lists of nested objects (
accounts,features, etc.) - โ Produces clean, Equatable-compatible models
- โ
Generates multiple
.dartfiles recursively - โ
Supports both:
- Command mode
- Interactive mode
- โ
Optional output folder support (
--out)
๐ฆ Installation #
Activate globally (recommended) #
dart pub global activate model_from_json
Now you can run:
model_from_json <json_path> --name ClassName
๐ Usage #
โ Command Mode #
Generate models from JSON in the current folder:
model_from_json complex.json --name ApiResponse
Generate models into a custom folder:
model_from_json complex.json --name ApiResponse --out lib/models
โ Interactive Mode #
Run without arguments:
model_from_json
It will prompt you:
Enter JSON file path: ultra.json
Enter root class name: ApiResponse
Enter output folder [.] : lib/models
๐งพ Example Input #
complex.json #
{
"user": {
"id": 1,
"name": "Dipesh"
},
"accounts": [
{
"broker": "zerodha",
"balance": 250000
}
]
}
โ Output Generated #
api_response.dart #
class ApiResponse extends Equatable {
final User user;
final List<Accounts> accounts;
const ApiResponse({
required this.user,
required this.accounts,
});
factory ApiResponse.fromJson(Map<String, dynamic> json) {
return ApiResponse(
user: User.fromJson(json['user'] ?? {}),
accounts: (json['accounts'] as List? ?? [])
.map((e) => Accounts.fromJson(e))
.toList(),
);
}
Map<String, dynamic> toJson() => {
'user': user.toJson(),
'accounts': accounts.map((e) => e.toJson()).toList(),
};
@override
List<Object?> get props => [user, accounts];
}
๐ Output Structure #
Example generated folder:
lib/models/
โโโ api_response.dart
โโโ user.dart
โโโ accounts.dart
โโโ broker_settings.dart
โ๏ธ CLI Options #
| Option | Description |
|---|---|
--name |
Root class name (required) |
--out |
Output folder (optional) |
--help |
Show usage help |
โ Roadmap (Upcoming) #
Planned improvements:
- Nullable type inference (
String? phone) - Dictionary/Map field support (
Map<String,dynamic> metadata) - Better naming rules (
daily_pnl โ DailyPnl) - Barrel export generation (
models.dart) - Pub.dev v1.0 stable release
๐ค Contributing #
Pull requests are welcome!
To contribute:
git clone https://github.com/yourusername/model_from_json.git
cd model_from_json
dart pub get
dart run
๐ License #
MIT License ยฉ 2026 Dipesh Panchal
โญ Support #
If you find this tool useful, consider starring the repo and sharing it with Flutter developers!