Flutter 常见编程规范介绍与报警后的解决方案

1.1 文件名称应该以小写字母加下划线的规则来命名(file_names)

警告示例:

import 'LogoWidget.dart';

警告:The file name 'LogoWidget.dart' isn't a lower_case_with_underscores identifier.Try changing the name to follow the lower_case_with_underscores style.

翻译:文件名“LogoWidget.dart”不是较低的_case_with_underscores标识符。尝试更改名称以遵循lower_case_with_underscores样式。

原因:

file_names规则要求:文件名称应该以小写字母加下划线的规则来命名。上面示例没有执行这个规则,所以报上面警告。

解决办法:

(1)按上面规则修改文件名称:

import 'logo_widget.dart';

(2)(推荐)如果不想按这个规则修改文件名称,需要在analysis_options.yaml上忽略这个规则:

analyzer:
  errors:
    file_names: ignore

1.2 常量名称使用小写字母开头的驼峰命名(constant_identifier_names)

警告示例:

enum ButtonState {
  BTN_NORMAL,
  BTN_PRESSED,
  BTN_HOVER,
}

警告:The variable name 'BTN_NORMAL' isn't a lowerCamelCase identifier.Try changing the name to follow the lowerCamelCase style.

翻译:变量名 BTN_NORMAL 不是较低的_case_with_underscores标识符。尝试更改名称以遵循lower_case_with_underscores样式。

原因:

constant_identifier_names规则要求:常量名称使用小写字母开头的驼峰命名。上面示例没有执行这个规则,所以报上面警告。

解决办法:

(1)按上面规则修改常量名称:

enum ButtonState {
  btnNormal,
  btnPressed,
  btnHover,
}

(2)(推荐)如果不想按规则修改,则在analysis_options.yaml上忽略这个规则:

analyzer:
  errors:
    constant_identifier_names: ignore

1.3 非常量变量应该使用小写字母命名规范(non_constant_identifier_names)

警告示例:

String Passwd = '1234';

警告:The variable name 'Passwd' isn't a lowerCamelCase identifier.Try changing the name to follow the lowerCamelCase style.

翻译:非常量变量 Passwd 不是较低的_case_with_underscores标识符。尝试更改名称以遵循lower_case_with_underscores样式。

原因:

non_constant_identifier_names规则要求:非常量变量应该使用小写字母命名规范。上面示例没有执行这个规则,所以报上面警告。

解决办法:

(1)(推荐)按上面规则修改非常量变量名称:

String passwd = '1234';

(2)如果不想按规则修改,则在analysis_options.yaml上忽略这个规则:

analyzer:
  errors:
    non_constant_identifier_names: ignore

1.4 使用const来修饰具有const初始化方法产生的对象(prefer_const_constructors)

警告示例:

Text("Hello, world!")

警告:Use 'const' with the constructor to improve performance.Try adding the 'const' keyword to the constructor invocation.

翻译:在构造函数中使用“const”以提高性能。尝试将“const”关键字添加到构造函数调用中。

原因:

prefer_const_constructors规则要求:使用 const 来修饰具有 const 初始化方法产生的对象。上面示例没有执行这个规则,所以报上面警告。

解决办法:

(1)(推荐)按上面规则修改:

const Text("Hello, world!")

(2)如果不想按规则修改,则在analysis_options.yaml上忽略这个规则:

analyzer:
  errors:
    prefer_const_constructors: ignore

1.5 在组件的构造函数中一定要使用key(use_key_in_widget_constructors)

警告示例:

class MyApp extends StatelessWidget { // MyApp这里警告
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: MyHomeBody(),
      ),
    );
  }
}

警告:Use key in widget constructors.

翻译:在小部件构造函数中使用键。

原因:

use_key_in_widget_constructors规则要求:在组件的构造函数中一定要使用 key。上面示例没有执行这个规则,所以报上面警告。

解决办法:

(1)(推荐)按上面规则修改:

class MyApp extends StatelessWidget {
  const MyApp({super.key}); // 使用key
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: MyHomeBody(),
      ),
    );
  }
}

(2)如果不想按规则修改,则在analysis_options.yaml上忽略这个规则:

analyzer:
  errors:
    use_key_in_widget_constructors: ignore

1.6 一个类被标记为@immutable,那么它的所有实例字段必须都是不可变的(must_be_immutable)

警告示例:

class MyWidget extends StatelessWidget {
  var _title = "Initial Title"; // 非final字段
  @override
  Widget build(BuildContext context) {
    return Text(_title);
  }
}

警告:This class (or a class that this class inherits from) is marked as '@immutable', but one or more of its instance fields aren't final

翻译:此类(或此类继承的类)标记为“@immutable”,但其一个或多个实例字段不是最终字段

原因:

must_be_immutable规则要求:一个类被标记为 @immutable,那么它的所有实例字段必须都是不可变的。上面示例没有执行这个规则,所以报上面警告。

解决办法:

(1)(推荐)变量定义前面加上final即可:

class MyWidget extends StatelessWidget {
  final String title = "Initial Title"; // 使用final
  @override
  Widget build(BuildContext context) {
    return Text(title);
  }
}

(2)如果不想按规范修改,则在analysis_options.yaml上忽略这个规则:

analyzer:
  errors:
    must_be_immutable: ignore

1.7 在线上代码中应该避免使用print(avoid_print)

警告示例:

print('Hello World!');

警告:Don't invoke 'print' in production code.

翻译:不要在生产代码中调用“print”。

原因:

avoid_print规则要求:在线上代码中应该避免使用 print。上面示例没有执行这个规则,所以报上面警告。

解决办法:

(1)(推荐)使用debugPrint替换即可:

debugPrint('Hello World!');

(2)如果不想按规范替换,则在analysis_options.yaml上忽略这个规则:

analyzer:
  errors:
    avoid_print: ignore

1.8 不要使用没有必要的container(avoid_unnecessary_containers)

警告示例:

Container(
  color: Colors.blue,
  child: Text('Hello, World!'),
);

警告:Unnecessary instance of 'Container'.Try removing the 'Container' (but not its children) from the widget tree.

翻译:“Container”的不必要实例。尝试从小部件树中删除“容器”(但不是其子项)。

原因:

constant_identifier_names规则要求:不要使用没有必要的container。上面示例没有执行这个规则,所以报上面警告。

解决办法

(1)(推荐)如果Container仅用于设置背景颜色或背景图片,可以使用colordecoration属性直接在具体的控件上设置背景:

Text(
  'Hello, World!',
  style: TextStyle(backgroundColor: Colors.blue),
);

(2)如果实在不想按规范使用,则在analysis_options.yaml上忽略这个规则:

analyzer:
  errors:
    avoid_unnecessary_containers: ignore

参考:

Flutter编程规范 - 简书


posted @   fengMisaka  阅读(133)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示