这篇文章摘自here https://stackoverflow.com/a/67753298/12483095有修改
最近,flutter 团队创建了一个新包,名为flutter_lints https://pub.dev/packages/flutter_lints并将其添加到您的analysis_options.yaml
默认情况下。
要了解有关 flutter lints 包的更多信息,请参阅此文档(flutter lints 包简介 https://flutter.dev/docs/release/breaking-changes/flutter-lints-package).
有几种方法可以删除这个prefer const with constant constructors
警告。
您可以在文件顶部添加忽略行注释。如果您想从文件中删除警告。
// ignore_for_file: prefer_const_constructors
import 'dart:async';
import 'package:flutter/material.dart';
否则,如果您想删除该特定行的警告,请在该行的顶部添加忽略行注释。
AppBar(
// ignore: prefer_const_constructors
title:Text('Register'),
),
这是禁用该规则的最简单方法。
另一种是您可以简单地从以下行中删除analysis_options.yaml
file.
include: package:flutter_lints/flutter.yaml
但最好保留这条线并在其中添加一些规则analysis_options.yaml
文件(这是我根据我的研究得出的个人意见)。
为了避免prefer const with constant constructors
警告添加此规则prefer_const_constructors : false
to the analysis_options.yaml
file.
linter:
rules:
prefer_const_constructors : false
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
还有其他规则可以帮助您使代码变得更好。如果需要,您可以添加以下一些规则(来自pedantic https://github.com/google/pedantic/blob/master/lib/analysis_options.1.11.0.yaml).
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