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 modelimport '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);
}
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
- database
- data_sources
- local
- products
- remote
- products
- products_remote_data_source.g.dart
- models
- repositories
- products
Feature-Based Architecture:
- xflutter_cli_modules_application
- android
- assets
- ios
- lib
- common
- data
- database
- modules
- products
- data
- data_sources
- local
- products
- remote
- products
- products_remote_data_source.g.dart
- models
- repositories
- products
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 data layer module path, used only with (modules-architecture) | modules/products |
entity-name | option | name of entity for generating the data layer | |
primary-key | option | specify local-database entity with name of primary property, default is `id` | |
end-point | option | add custom end-point to APIs, by default the APIs endpoint is the same of entity-name | shop/products |
local-database | flag | generate local database with remote data | |
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 |