Generate Data Layer

Generate Remote Data and Offline Data layers for @freezed model.The Data layer will generated for specific model that exists in your project.
xflutter_cli generate remote

Scenario study:

We have a Product @freezed model and we want to generate (create, read, update and delete) APIs for this model
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({
    int? id,
    String? title,
    String? description,
    num? price,
    num? rating,
    List<String>? tags,
    String? brand,
    Dimensions? dimensions,
    List<Review>? reviews,
    String? returnPolicy,
    int? minimumOrderQuantity,
    Meta? meta,
    List<String>? images,
    String? thumbnail,
  }) = _Product;

  factory Product.fromJson(Map<String, dynamic> json) => _$ProductFromJson(json);
}

Generate new data layer 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 data layer.

Check monorepo drawbacks

Select Model

  • If you generate the model by the CLI, it will generate the model in correct place automatically.
  • The CLI will search for all entities in `**/models/entities/**/*.dart`, so make sure you put the model in the correct place.
    for more info see: glob

Remote Data

  • CLI depends on retrofit and dio packages.
  • Configuration like interceptors and environments will be generated automatically.
  • Be sure that (app_injectable for application) or (core_injectable for monorepo-workspace) is called in your main.dart, for more info see: injectable
  • Configuration depends on injectable and it is auto generated by CLI,
    you can see (app_module & app_injectable) for application and (core_module & core_injectable) for monorepo-workspace

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
        • database
          • app_store.dart
        • data_sources
          • local
            • products
              • products_local_data_source.dart
              • products_local_data_source_impl.dart
          • remote
            • products
              • products_remote_data_source.dart
              • products_remote_data_source.g.dart
        • models
        • repositories
          • products
            • products_repository.dart
            • products_repository_impl.dart
      • 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
      • common
        • data
          • database
            • app_store.dart
      • modules
        • products
          • data
            • data_sources
              • local
                • products
                  • products_local_data_source.dart
                  • products_local_data_source_impl.dart
              • remote
                • products
                  • products_remote_data_source.dart
                  • products_remote_data_source.g.dart
            • models
            • repositories
              • products
                • products_repository.dart
                • products_repository_impl.dart
      • 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 data layer module path, used only with (modules-architecture)modules/products
entity-nameoptionname of entity for generating the data layer
primary-keyoptionspecify local-database entity with name of primary property, default is `id`
end-pointoptionadd custom end-point to APIs, by default the APIs endpoint is the same of entity-nameshop/products
local-databaseflaggenerate local database with remote data
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