Flutter代码生成插件arquitecture_cmi_generators的使用

Flutter代码生成插件arquitecture_cmi_generators的使用

CMI架构生成器

使用该插件可以生成一个遵循CMI架构的项目。 CMI版本:v0.1.0 GitHub仓库

目录

安装

请注意,此包应作为开发依赖项添加,而不是在dependencies中安装。这是因为这是一个开发工具。

  1. 在你的pubspec.yaml文件中添加architecture_cmi_generators作为开发依赖项:
dev_dependencies:
  arquitecture_cmi_generators: 0.1.6
  1. 运行以下命令以安装该包:
dart pub get

使用

1. CMI架构

要使用CMI架构构建项目,请运行以下命令:

dart run arquitecture_cmi_generators build

这将创建一个新的文件夹和文件结构,用于CMI架构。

文件夹结构

当你运行上述命令后,会生成如下的文件夹结构:

my_project/
├── lib/
│   ├── core/
│   │   ├── utils/
│   │   └── services/
│   ├── features/
│   │   ├── feature_name/
│   │   │   ├── data/
│   │   │   │   ├── models/
│   │   │   │   └── repositories/
│   │   │   ├── domain/
│   │   │   │   ├── entities/
│   │   │   │   └── usecases/
│   │   │   └── presentation/
│   │   │       ├── pages/
│   │   │       ├── viewmodels/
│   │   │       └── widgets/
│   ├── main.dart
└── pubspec.yaml

每个文件夹的具体作用如下:

  • core/utils: 存放通用工具类。
  • core/services: 存放服务类。
  • features/feature_name/data/models: 存放数据模型。
  • features/feature_name/data/repositories: 存放数据仓库。
  • features/feature_name/domain/entities: 存放实体类。
  • features/feature_name/domain/usecases: 存放用例类。
  • features/feature_name/presentation/pages: 存放页面。
  • features/feature_name/presentation/viewmodels: 存放视图模型。
  • features/feature_name/presentation/widgets: 存放小部件。

许可证

版权所有 © 2023 arquitecture_cmi_generators,由UTOQINGAPP持有版权。

本软件根据Apache License, Version 2.0许可协议授权。除非适用法律或书面同意,否则软件按“原样”分发,不附带任何担保或条件,无论是明示还是暗示。有关特定语言权限和限制的详细信息,请参阅许可证文本。

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

更多关于Flutter代码生成插件arquitecture_cmi_generators的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter代码生成插件arquitecture_cmi_generators的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


arquitecture_cmi_generators 是一个用于 Flutter 的代码生成插件,旨在帮助开发者自动生成常见的架构代码,如模型类、服务类、存储库类等。这个插件可以显著减少手动编写重复代码的工作量,提高开发效率。

以下是使用 arquitecture_cmi_generators 的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 arquitecture_cmi_generators 作为开发依赖。

dev_dependencies:
  arquitecture_cmi_generators: ^1.0.0  # 请使用最新版本
  build_runner: ^2.0.0

2. 创建模型类

假设你有一个 JSON 数据,你想要生成对应的 Dart 模型类。你可以创建一个 Dart 文件,并添加一些注解来指定如何生成代码。

import 'package:arquitecture_cmi_generators/arquitecture_cmi_generators.dart';

part 'user_model.g.dart';

@JsonSerializable()
class User {
  final String name;
  final int age;
  final String email;

  User({required this.name, required this.age, required this.email});

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);
}

3. 运行代码生成器

使用 build_runner 来生成代码。在终端中运行以下命令:

flutter pub run build_runner build

这将生成 user_model.g.dart 文件,其中包含 User 类的 fromJsontoJson 方法。

4. 使用生成的代码

现在你可以在你的项目中使用生成的代码了。例如:

void main() {
  final json = {
    'name': 'John Doe',
    'age': 30,
    'email': 'john.doe@example.com',
  };

  final user = User.fromJson(json);
  print(user.name);  // 输出: John Doe
}

5. 其他功能

arquitecture_cmi_generators 可能还提供了其他功能,如生成服务类、存储库类等。你可以查阅插件的文档以了解更多详细信息。

6. 清理生成的文件

如果你想清理生成的文件,可以运行以下命令:

flutter pub run build_runner clean
回到顶部