Generate Data Models
Generate Freezed Entity Model from http response.xflutter_cli generate model
Scenario study:
We have a Http Api returns Product model as json and we want to convert this json to @freezed model{
"message": "Success",
"statusCode": 200,
"success": true,
"data": {
"total": 194,
"list": [
{
"id": 1,
"title": "Essence Mascara Lash Princess",
"description": "The Essence Mascara Lash Princess is a popular mascara known for its volumizing and lengthening effects.",
"category": "beauty",
"price": 9.99,
"discountPercentage": 7.17,
"rating": 4.94,
"stock": 5,
"tags": [
"beauty",
"mascara"
],
"brand": "Essence",
"sku": "RCH45Q1A",
"weight": 2,
"dimensions": {
"width": 23.17,
"height": 14.43,
"depth": 28.01
},
"warrantyInformation": "1 month warranty",
"shippingInformation": "Ships in 1 month",
"availabilityStatus": "Low Stock",
"reviews": [
{
"rating": 2,
"comment": "Very unhappy with my purchase!",
"date": "2024-05-23T08:56:21.618Z",
"reviewerName": "John Doe",
"reviewerEmail": "john.doe@x.dummyjson.com"
}
],
"returnPolicy": "30 days return policy",
"minimumOrderQuantity": 24,
"meta": {
"createdAt": "2024-05-23T08:56:21.618Z",
"updatedAt": "2024-05-23T08:56:21.618Z",
"barcode": "9164035109868",
"qrCode": "https://assets.dummyjson.com/public/qr-code.png"
},
"images": [
"https://cdn.dummyjson.com/products/images/beauty/Essence%20Mascara%20Lash%20Princess/1.png"
],
"thumbnail": "https://cdn.dummyjson.com/products/images/beauty/Essence%20Mascara%20Lash%20Princess/thumbnail.png"
}
]
}
}
Generated Code:
import 'package:freezed_annotation/freezed_annotation.dart';
import '../dimensions/dimensions.dart';
import '../review/review.dart';
import '../meta/meta.dart';
part 'product.freezed.dart';
part 'product.g.dart';
@freezed
class Product with _$Product {
const factory Product({
@JsonKey(name: 'id') int? id,
@JsonKey(name: 'title') String? title,
@JsonKey(name: 'description') String? description,
@JsonKey(name: 'category') String? category,
@JsonKey(name: 'price') num? price,
@JsonKey(name: 'discountPercentage') num? discountPercentage,
@JsonKey(name: 'rating') num? rating,
@JsonKey(name: 'stock') int? stock,
@JsonKey(name: 'tags') List<String>? tags,
@JsonKey(name: 'brand') String? brand,
@JsonKey(name: 'sku') String? sku,
@JsonKey(name: 'weight') int? weight,
@JsonKey(name: 'dimensions') Dimensions? dimensions,
@JsonKey(name: 'warrantyInformation') String? warrantyInformation,
@JsonKey(name: 'shippingInformation') String? shippingInformation,
@JsonKey(name: 'availabilityStatus') String? availabilityStatus,
@JsonKey(name: 'reviews') List<Review>? reviews,
@JsonKey(name: 'returnPolicy') String? returnPolicy,
@JsonKey(name: 'minimumOrderQuantity') int? minimumOrderQuantity,
@JsonKey(name: 'meta') Meta? meta,
@JsonKey(name: 'images') List<String>? images,
@JsonKey(name: 'thumbnail') String? thumbnail,
}) = _Product;
factory Product.fromJson(Map<String, dynamic> json) => _$ProductFromJson(json);
}
Result:
CLI provides two options to create the Flutter application Layer-Based Architecture and Feature-Based ArchitectureLayer-Based Architecture:
- xflutter_cli_test_application
- android
- assets
- ios
- lib
- data
- models
- entities
- dimensions
- meta
- product
- review
Feature-Based Architecture:
- xflutter_cli_modules_application
- android
- assets
- ios
- lib
- modules
- products
- data
- models
- entities
- dimensions
- meta
- product
- review
Usage:
Name | Type | Description | Example |
---|---|---|---|
src | option | specify one of the modules to execute command in (monorepo workspace or standalone modular-app) | |
path | option | generate model path, used only with (modules-architecture) | |
entity-path | option | http api end-point | |
entity-name | option | specify entity className | |
json-key | option | generate model from specific key, this option is helpful when your model is nested object in json | |
base-dir | option | generate all/some models in base common directory, used only with (modules-architecture) | dimensions,reviews |
authorization | option | add authorization to your http request headers, Api maybe require authorization for return success response, in this case you need this option | Bearer your_jwt_token |
add-entity-prefix | flag | add prefix to all entity nested entities, ex: [Product -> Rating] will be [Product -> ProductRating] | |
source | option | import file from specific module, this is helpful when you have multiple files with same name and one of them should be imported in the generated file, so you need to tell CLI which one should be imported | filename.dart:my_app1 |
sources | option | import file from multiple modules, this is helpful when you have multiple files with same name and many of them of them should be imported in the generated file, so you need to tell CLI which files should be imported | models.dart:my_package1, models.dart:my_package2 |