Flutter代码规范与约定插件glory_convention_lint的使用
Flutter代码规范与约定插件glory_convention_lint的使用
GloryConventionLint-Flutter
GloryConventionLint是支持IDE Android Studio/Visual Studio Code的Flutter代码规范检查插件。
Setup
添加插件到pubspec.yaml
$ flutter pub add --dev glory_convention_lint
在pubspec.yaml
文件中添加以下依赖:
dev_dependencies:
glory_convention_lint: ^1.0.0
custom_lint: ^0.2.5
添加分析器插件到analysis_options.yaml
在analysis_options.yaml
文件中添加以下内容:
analyzer:
plugins:
- custom_lint
规范
模型规范
模型类名规范
确保模型类名以Model
结尾。
// 正确
class ProductModel {}
// 错误
class ProductModel {}
模型文件名规范
模型文件名必须以_model.dart
结尾。
// 正确
product_model.dart
// 错误
product.dart
productmodel.dart
模型注解规范
在模型类前添加@JsonSerializable()
注解。
// 正确
@JsonSerializable()
class ProductModel {
int? id;
}
// 错误
class ProductModel {
int? id;
}
@JsonSerializable()
建议模型字段使用可空类型
建议模型字段使用可空类型,例如String?
而不是String
。
// 正确
class Product {
String? name;
Product({this.name});
}
// 错误
class Product {
String name;
Product({this.name});
}
服务规范
服务类名规范
确保服务类名以Services
结尾。
// 正确
class GiftServices{}
class ProductServices{}
// 错误
class Gift{}
class ProductService{} // 单数而非复数
服务文件名规范
服务文件名必须以service.dart
结尾。
// 正确
gift_services.dart
product_services.dart
// 错误
product_service.dart // 单数而非复数
ProductRequest.dart
服务注解规范
在服务类前添加@RestApi()
注解。
// 正确
@RestApi() // 添加了RestApi注解
abstract class ProductServices {}
// 错误
// 忘记添加RestApi注解
abstract class ProductServices {}
枚举规范
枚举类名规范
确保枚举类名以Enum
结尾。
// 正确
enum AvatarEnum {}
// 错误
enum EnumAvatar {}
枚举文件名规范
确保枚举文件名以_enum.dart
结尾。
// 正确
gift_enum.dart
product_enum.dart
// 错误
ProductEnum.dart
请求规范
请求类名规范
请求类名总是以Request
结尾,并且必须使用PascalCase。
// 正确
class GiftRequest{}
class ProductRequest{}
// 错误
class Gift{}
class product_request{}
请求文件名规范
请求文件名必须以_request
结尾,并且必须使用snake_case。
// 正确
product_request.dart
// 错误
ProductRequest.dart
请求文件必须放在request
目录下。
|- data
|- network
|- request
响应规范
响应类名规范
响应类名总是以Response
结尾,并且必须使用PascalCase。
// 正确
class GiftResponse{}
class ProductResponse{}
// 错误
class Gift{}
class product_response{}
响应文件名规范
响应文件名必须以_response
结尾,并且必须使用snake_case。
// 正确
product_response.dart
// 错误
ProductResponse.dart
响应文件必须放在response
目录下。
|- data
|- network
|- response
其他规范
命名规范
类别 | PascalCase | CamelCase | Plural | SnakeCase | 示例 |
---|---|---|---|---|---|
类 | ✅ | class ModelResponse {} |
|||
服务类 | ✅ | ✅ | class ModelServices {} |
||
常量类 | ✅ | ✅ | class NetworkConstants {} |
||
扩展 | ✅ | ✅ | extension StringExtensions on String |
||
字段 | ✅ | int id; |
|||
变量 | ✅ | int variable; |
|||
局部变量 | ✅ | ✅ | int _variable; |
||
参数 | ✅ | String param |
|||
方法 | ✅ | void methodName() {} |
|||
局部方法 | ✅ | ✅ | void _methodName() {} |
||
枚举类型 | ✅ | enum Status {} |
单个文件只包含一个类的规范
避免在一个文件中声明多个类。最好每个文件只声明一个类,以减少混淆。
// 正确
-- test.dart --
class One {}
// 错误
-- test.dart --
class One {}
class Two {}
静态常量语言变量规范
变量应声明为静态常量。
// 正确
class One {
static const variableOne = "Value";
}
// 错误
class One {
String variableOne = "Value";
}
基础响应导入规范
BaseResponse
和BaseListResponse
必须从rollingglory_codebase
导入。
当应用程序通过API调用与后端通信时,我们通常会收到两种类型的响应:单个对象和多个对象。这两种类型都需要在服务文件中实现,服务文件实际上是一个抽象类,包含了获取数据所需的一组方法。
// 正确
class One {
Future<BaseListResponse<Episode>> getEpisodes();
Future<BaseResponse<Episode>> getEpisodeDetail();
}
// 错误
class One {
Future<Episode> myMethod();
}
一个类只有一个语言变量规范
确保表示语言的变量被分开,一个类应该只有一个变量。
// 正确
-- languages/id_lang.dart --
Map<String, String> id = {};
-- languages/en_lang.dart --
Map<String, String> en = {};
// 错误
-- languages.dart --
Map<String, String> id = {};
Map<String, String> en = {};
更多关于Flutter代码规范与约定插件glory_convention_lint的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter代码规范与约定插件glory_convention_lint的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
glory_convention_lint
是一个用于 Flutter 项目的代码规范和约定检查的插件。它可以帮助开发者在项目中自动执行一些代码规范检查,确保代码风格一致,减少潜在的错误。以下是使用 glory_convention_lint
插件的步骤和注意事项。
1. 安装 glory_convention_lint
插件
首先,你需要在 pubspec.yaml
文件中添加 glory_convention_lint
作为开发依赖项。
dev_dependencies:
glory_convention_lint: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 配置 analysis_options.yaml
接下来,你需要在项目的根目录下创建或修改 analysis_options.yaml
文件,以启用 glory_convention_lint
的规则。
include: package:glory_convention_lint/analysis_options.yaml
# 你可以在这里添加或覆盖一些规则
analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
errors:
unused_element: warning
unused_import: warning
unused_local_variable: warning
linter:
rules:
# 你可以在这里添加或覆盖一些规则
avoid_print: true
prefer_const_constructors: true
prefer_final_fields: true
3. 运行代码分析
你可以通过以下命令来运行代码分析,检查项目中是否符合 glory_convention_lint
的规范:
flutter analyze
4. 集成到 CI/CD
为了确保代码规范在每次提交或合并时都得到检查,你可以将 flutter analyze
集成到你的 CI/CD 流程中。例如,在 GitHub Actions 中添加一个步骤:
name: Flutter CI
on: [push, pull_request]
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: subosito/flutter-action@v2
with:
flutter-version: '3.0.0' # 使用你项目中的 Flutter 版本
- run: flutter pub get
- run: flutter analyze
5. 自定义规则
glory_convention_lint
提供了一些默认的规则,但你可以根据项目的需求自定义这些规则。你可以在 analysis_options.yaml
文件中添加或覆盖规则。例如:
linter:
rules:
avoid_print: true
prefer_const_constructors: true
prefer_final_fields: true
# 添加更多规则