• Generate with http response
  • Generate with json file

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"
      }
    ]
  }
}

Generate new model inside workspace

  • Make sure to run the command in the workspace root directory instead of (application or package) directory.
  • Use the src option to specify the (app or package) in which you want to generate the model.

Model Name Suffix

  • When you name a model with a `Response` suffix, it will be placed in the /responses directory.
  • When you name a model with a `Request` suffix, it will be placed in the /requests directory.

@freezed configuration

The build.yaml file contains the build modification configuration.for more info see: freezed behavior

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 Architecture

Layer-Based Architecture:

  • xflutter_cli_test_application
    • android
    • assets
    • ios
    • lib
      • data
        • models
          • entities
            • dimensions
            • meta
            • product
            • review
      • main.dart
    • analysis_options.yaml
    • build.yaml
    • flutter_launcher_icons.yaml
    • flutter_native_splash.yaml
    • pubspec.yaml
    • xflutter_cli.yaml

Feature-Based Architecture:

  • xflutter_cli_modules_application
    • android
    • assets
    • ios
    • lib
      • modules
        • products
          • data
            • models
              • entities
                • dimensions
                • meta
                • product
                • review
      • main.dart
    • analysis_options.yaml
    • build.yaml
    • flutter_launcher_icons.yaml
    • flutter_native_splash.yaml
    • pubspec.yaml
    • xflutter_cli.yaml

Usage:

Name
Type
Description
Example
srcoptionspecify one of the modules to execute command in (monorepo workspace or standalone modular-app)
pathoptiongenerate model path, used only with (modules-architecture)
entity-pathoptionhttp api end-point
entity-nameoptionspecify entity className
json-keyoptiongenerate model from specific key, this option is helpful when your model is nested object in json
base-diroptiongenerate all/some models in base common directory, used only with (modules-architecture)dimensions,reviews
authorizationoptionadd authorization to your http request headers, Api maybe require authorization for return success response, in this case you need this optionBearer your_jwt_token
add-entity-prefixflagadd prefix to all entity nested entities, ex: [Product -> Rating] will be [Product -> ProductRating]
sourceoptionimport 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 importedfilename.dart:my_app1
sourcesoptionimport 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 importedmodels.dart:my_package1, models.dart:my_package2