Generate Offline

Generate Offline Data layers for @freezed model.
xflutter_cli generate offline

Scenario study:

We have a Category @freezed model and we want to generate (create, read, update and delete) caching logic for this model
import 'package:freezed_annotation/freezed_annotation.dart';
import '../media/media.dart';
part 'category.freezed.dart';
part 'category.g.dart';

@freezed
class Category with _$Category {
  const factory Category({
    int? id,
    String? name,
    List<Media>? media,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) = _Category;

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

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

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

Check monorepo drawbacks

Offline Data

  • CLI depends on objectbox package.
  • Configuration like 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
        • data_sources
          • local
            • categories
              • categories_local_data_source.dart
              • categories_local_data_source_impl.dart
        • models
          • entities
            • category
        • repositories
          • categories
            • categories_repository.dart
            • categories_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
      • modules
        • categories
          • data
            • data_sources
              • local
                • categories
                  • categories_local_data_source.dart
                  • categories_local_data_source_impl.dart
            • models
            • repositories
              • categories
                • categories_repository.dart
                • categories_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/categories
entity-nameoptionname of entity for generating the data layer
primary-keyoptionspecify local-database entity with name of primary property, default is `id`
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