"Use key in widget constructors" 每次创建新小部件时都会发出警告
"Use key in widget constructors" warning each time I create a new widget
我每次创建新 class 时都会收到警告。
Use key in widget constructors.
Prefer const with constant constructor.
等等。这些错误是从哪里来的,我该如何摆脱它?
解决方案 1:
如果您不想忽略警告,只需执行 super.key
FooPage({super.key});
解决方案 2:
打开位于项目根目录的 analysis_options.yaml
文件:
include: package:flutter_lints/flutter.yaml
# Add these lines
linter:
rules:
use_key_in_widget_constructors: false
解决方案 3:
要禁用特定文件的规则,请将此行放在代码中的任意位置。
// ignore_for_file: prefer_const_constructors
解决方案 4:
正如@Sigiria 在评论中所建议的那样,您可以 运行:
dart fix --apply
解决方案 5:
最近 Flutter 团队创建了一个新包 flutter_lints
,它默认添加到您的 analysis_options.yaml
新创建项目的文件中。
您只需从 analysis_options.yaml
文件中删除以下行:
include: package:flutter_lints/flutter.yaml
但是,我建议您至少在其中添加一些规则,例如,您可以添加这些规则(来自 pedantic
)并删除您不想要的规则。
linter:
rules:
- always_declare_return_types
- always_require_non_null_named_parameters
- annotate_overrides
- avoid_init_to_null
- avoid_null_checks_in_equality_operators
- avoid_relative_lib_imports
- avoid_return_types_on_setters
- avoid_shadowing_type_parameters
- avoid_single_cascade_in_expression_statements
- avoid_types_as_parameter_names
- await_only_futures
- camel_case_extensions
- curly_braces_in_flow_control_structures
- empty_catches
- empty_constructor_bodies
- library_names
- library_prefixes
- no_duplicate_case_values
- null_closures
- omit_local_variable_types
- prefer_adjacent_string_concatenation
- prefer_collection_literals
- prefer_conditional_assignment
- prefer_contains
- prefer_equal_for_default_values
- prefer_final_fields
- prefer_for_elements_to_map_fromIterable
- prefer_generic_function_type_aliases
- prefer_if_null_operators
- prefer_inlined_adds
- prefer_is_empty
- prefer_is_not_empty
- prefer_iterable_whereType
- prefer_single_quotes
- prefer_spread_collections
- recursive_getters
- slash_for_doc_comments
- sort_child_properties_last
- type_init_formals
- unawaited_futures
- unnecessary_brace_in_string_interps
- unnecessary_const
- unnecessary_getters_setters
- unnecessary_new
- unnecessary_null_in_if_null_operators
- unnecessary_this
- unrelated_type_equality_checks
- unsafe_html
- use_full_hex_values_for_flutter_colors
- use_function_type_syntax_for_parameters
- use_rethrow_when_possible
- valid_regexps
您可以在 analysis_options.yaml
中使用 prefer_const_constructors : false
将此添加到您的代码中,就在@override 行之前
const FooPage({Key? key}) : super(key: key);
始终在 EdgeInsets、spacer、SizedBox、BorderRadius 等小部件命令前添加常量
我每次创建新 class 时都会收到警告。
Use key in widget constructors.
Prefer const with constant constructor.
等等。这些错误是从哪里来的,我该如何摆脱它?
解决方案 1:
如果您不想忽略警告,只需执行 super.key
FooPage({super.key});
解决方案 2:
打开位于项目根目录的 analysis_options.yaml
文件:
include: package:flutter_lints/flutter.yaml
# Add these lines
linter:
rules:
use_key_in_widget_constructors: false
解决方案 3:
要禁用特定文件的规则,请将此行放在代码中的任意位置。
// ignore_for_file: prefer_const_constructors
解决方案 4:
正如@Sigiria 在评论中所建议的那样,您可以 运行:
dart fix --apply
解决方案 5:
最近 Flutter 团队创建了一个新包 flutter_lints
,它默认添加到您的 analysis_options.yaml
新创建项目的文件中。
您只需从 analysis_options.yaml
文件中删除以下行:
include: package:flutter_lints/flutter.yaml
但是,我建议您至少在其中添加一些规则,例如,您可以添加这些规则(来自 pedantic
)并删除您不想要的规则。
linter:
rules:
- always_declare_return_types
- always_require_non_null_named_parameters
- annotate_overrides
- avoid_init_to_null
- avoid_null_checks_in_equality_operators
- avoid_relative_lib_imports
- avoid_return_types_on_setters
- avoid_shadowing_type_parameters
- avoid_single_cascade_in_expression_statements
- avoid_types_as_parameter_names
- await_only_futures
- camel_case_extensions
- curly_braces_in_flow_control_structures
- empty_catches
- empty_constructor_bodies
- library_names
- library_prefixes
- no_duplicate_case_values
- null_closures
- omit_local_variable_types
- prefer_adjacent_string_concatenation
- prefer_collection_literals
- prefer_conditional_assignment
- prefer_contains
- prefer_equal_for_default_values
- prefer_final_fields
- prefer_for_elements_to_map_fromIterable
- prefer_generic_function_type_aliases
- prefer_if_null_operators
- prefer_inlined_adds
- prefer_is_empty
- prefer_is_not_empty
- prefer_iterable_whereType
- prefer_single_quotes
- prefer_spread_collections
- recursive_getters
- slash_for_doc_comments
- sort_child_properties_last
- type_init_formals
- unawaited_futures
- unnecessary_brace_in_string_interps
- unnecessary_const
- unnecessary_getters_setters
- unnecessary_new
- unnecessary_null_in_if_null_operators
- unnecessary_this
- unrelated_type_equality_checks
- unsafe_html
- use_full_hex_values_for_flutter_colors
- use_function_type_syntax_for_parameters
- use_rethrow_when_possible
- valid_regexps
您可以在 analysis_options.yaml
prefer_const_constructors : false
将此添加到您的代码中,就在@override 行之前
const FooPage({Key? key}) : super(key: key);
始终在 EdgeInsets、spacer、SizedBox、BorderRadius 等小部件命令前添加常量