flutter 代码规范之analysis_options

前言

没有规矩,不成方圆。不管我们在写什么程序时,都需要对我们的代码进行规范处理。
比如我们的css规范,html规范,js规范......
我们的flutter也不例外,也有要有规范的!
如果不按照规范来,可能会出现各种各样的问题,而且维护起来,说不定会让人怀疑人生。
今天这边文章的出现就是由于我没有按照规范来写!程序在高帧率手机下会出现失帧的问题。

Style linter for Dart

说是Flutter的规范,其实是Dart语言的代码规范,具体规范点连接进去查看,我也正在学习中。。。

analysis_options.yaml

各种资料查阅后发现,flutter团队已经给我们制定好规范了,对于开发者来说,你使用的是什么分支(branch)的Flutter SDK进行开发,你就可以直接使用当前分支下的flutter SKD下的analysis_options.yaml文件,将此文件复制到你项目的根目录中,然后你会发现,卧槽,裂开!
为什么这么说呢?请看下文。


image.png

接受制裁吧!

当你把analysis_options.yaml文件复制到你根目录的时候,你会发现程序的错误突然变成了N+
请看大屏幕


image.png

对,你没看错,这是我的程序,没放上去的时候,什么都没有,当把analysis_options.yaml放在项目的根目录后,就是这个样子了,骚年们,看到这3K+的问题,就问你裂开不裂开就完了。怎么办呢?裂开也没用啊,修复吧!

修复

根据提示修复

在提示规则的链接,点击一下,会直接进入到有问题的代码哪里,然后鼠标放上去会弹出提示,然后根据提示进行修复就可以了!

  • 第一步
    image.png
  • 第二步


    image.png
  • 第三步

点击快速修复,此时你可能又有点裂开,提示说让你忽略,添加忽略头,那岂不是瞎折腾了么,不行,接收不了。


image.png
  • 第四步

接受不了怎么办呢?还能怎么办,根据提示搞呗!


image.png

不多,也就几千行,这么搞也不一定会累死!哈哈哈

利用VSCode快速修复

在提示有问题的代码的地方command + ., 就会自动弹出快速修复,比如图中为增加const标识。


image.png

然后直接回车就ok了。这样是不是快很多了呢?

analysis_options.yaml文件的分析

先上整体源代码

# Specify analysis options.
#
# Until there are meta linter rules, each desired lint must be explicitly enabled.
# See: https://github.com/dart-lang/linter/issues/288
#
# For a list of lints, see: http://dart-lang.github.io/linter/lints/
# See the configuration guide for more
# https://github.com/dart-lang/sdk/tree/master/pkg/analyzer#configuring-the-analyzer
#
# There are other similar analysis options files in the flutter repos,
# which should be kept in sync with this file:
#
#   - analysis_options.yaml (this file)
#   - packages/flutter/lib/analysis_options_user.yaml
#   - https://github.com/flutter/plugins/blob/master/analysis_options.yaml
#   - https://github.com/flutter/engine/blob/master/analysis_options.yaml
#
# This file contains the analysis options used by Flutter tools, such as IntelliJ,
# Android Studio, and the `flutter analyze` command.

analyzer:
  strong-mode:
    implicit-casts: false
    implicit-dynamic: false
  errors:
    # treat missing required parameters as a warning (not a hint)
    missing_required_param: warning
    # treat missing returns as a warning (not a hint)
    missing_return: warning
    # allow having TODOs in the code
    todo: ignore
    # allow self-reference to deprecated members (we do this because otherwise we have
    # to annotate every member in every test, assert, etc, when we deprecate something)
    deprecated_member_use_from_same_package: ignore
    # Ignore analyzer hints for updating pubspecs when using Future or
    # Stream and not importing dart:async
    # Please see https://github.com/flutter/flutter/pull/24528 for details.
    sdk_version_async_exported_from_core: ignore
    # Turned off until null-safe rollout is complete.
    unnecessary_null_comparison: ignore
  exclude:
    - "bin/cache/**"

linter:
  rules:
    # these rules are documented on and in the same order as
    # the Dart Lint rules page to make maintenance easier
    # https://github.com/dart-lang/linter/blob/master/example/all.yaml
    - always_declare_return_types
    - always_put_control_body_on_new_line
    # - always_put_required_named_parameters_first # we prefer having parameters in the same order as fields https://github.com/flutter/flutter/issues/10219
    - always_require_non_null_named_parameters
    - always_specify_types
    # - always_use_package_imports # we do this commonly
    - annotate_overrides
    # - avoid_annotating_with_dynamic # conflicts with always_specify_types
    - avoid_bool_literals_in_conditional_expressions
    # - avoid_catches_without_on_clauses # we do this commonly
    # - avoid_catching_errors # we do this commonly
    - avoid_classes_with_only_static_members
    # - avoid_double_and_int_checks # only useful when targeting JS runtime
    # - avoid_dynamic_calls # not yet tested
    - avoid_empty_else
    - avoid_equals_and_hash_code_on_mutable_classes
    # - avoid_escaping_inner_quotes # not yet tested
    - avoid_field_initializers_in_const_classes
    - avoid_function_literals_in_foreach_calls
    # - avoid_implementing_value_types # not yet tested
    - avoid_init_to_null
    # - avoid_js_rounded_ints # only useful when targeting JS runtime
    - avoid_null_checks_in_equality_operators
    # - avoid_positional_boolean_parameters # not yet tested
    # - avoid_print # not yet tested
    # - avoid_private_typedef_functions # we prefer having typedef (discussion in https://github.com/flutter/flutter/pull/16356)
    # - avoid_redundant_argument_values # not yet tested
    - avoid_relative_lib_imports
    - avoid_renaming_method_parameters
    - avoid_return_types_on_setters
    # - avoid_returning_null # there are plenty of valid reasons to return null
    # - avoid_returning_null_for_future # not yet tested
    - avoid_returning_null_for_void
    # - avoid_returning_this # there are plenty of valid reasons to return this
    # - avoid_setters_without_getters # not yet tested
    - avoid_shadowing_type_parameters
    - avoid_single_cascade_in_expression_statements
    - avoid_slow_async_io
    - avoid_type_to_string
    - avoid_types_as_parameter_names
    # - avoid_types_on_closure_parameters # conflicts with always_specify_types
    - avoid_unnecessary_containers
    - avoid_unused_constructor_parameters
    - avoid_void_async
    # - avoid_web_libraries_in_flutter # not yet tested
    - await_only_futures
    - camel_case_extensions
    - camel_case_types
    - cancel_subscriptions
    # - cascade_invocations # not yet tested
    - cast_nullable_to_non_nullable
    # - close_sinks # not reliable enough
    # - comment_references # blocked on https://github.com/dart-lang/linter/issues/1142
    # - constant_identifier_names # needs an opt-out https://github.com/dart-lang/linter/issues/204
    - control_flow_in_finally
    # - curly_braces_in_flow_control_structures # not required by flutter style
    - deprecated_consistency
    # - diagnostic_describe_all_properties # not yet tested
    - directives_ordering
    # - do_not_use_environment # we do this commonly
    - empty_catches
    - empty_constructor_bodies
    - empty_statements
    - exhaustive_cases
    - file_names
    - flutter_style_todos
    - hash_and_equals
    - implementation_imports
    # - invariant_booleans # too many false positives: https://github.com/dart-lang/linter/issues/811
    - iterable_contains_unrelated_type
    # - join_return_with_assignment # not required by flutter style
    - leading_newlines_in_multiline_strings
    - library_names
    - library_prefixes
    # - lines_longer_than_80_chars # not required by flutter style
    - list_remove_unrelated_type
    # - literal_only_boolean_expressions # too many false positives: https://github.com/dart-lang/sdk/issues/34181
    - missing_whitespace_between_adjacent_strings
    - no_adjacent_strings_in_list
    # - no_default_cases # too many false positives
    - no_duplicate_case_values
    - no_logic_in_create_state
    # - no_runtimeType_toString # ok in tests; we enable this only in packages/
    - non_constant_identifier_names
    - null_check_on_nullable_type_parameter
    - null_closures
    # - omit_local_variable_types # opposite of always_specify_types
    # - one_member_abstracts # too many false positives
    # - only_throw_errors # https://github.com/flutter/flutter/issues/5792
    - overridden_fields
    - package_api_docs
    - package_names
    - package_prefixed_library_names
    # - parameter_assignments # we do this commonly
    - prefer_adjacent_string_concatenation
    - prefer_asserts_in_initializer_lists
    # - prefer_asserts_with_message # not required by flutter style
    - prefer_collection_literals
    - prefer_conditional_assignment
    - prefer_const_constructors
    - prefer_const_constructors_in_immutables
    - prefer_const_declarations
    - prefer_const_literals_to_create_immutables
    # - prefer_constructors_over_static_methods # far too many false positives
    - prefer_contains
    # - prefer_double_quotes # opposite of prefer_single_quotes
    - prefer_equal_for_default_values
    # - prefer_expression_function_bodies # conflicts with https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#consider-using--for-short-functions-and-methods
    - prefer_final_fields
    - prefer_final_in_for_each
    - prefer_final_locals
    - prefer_for_elements_to_map_fromIterable
    - prefer_foreach
    - prefer_function_declarations_over_variables
    - prefer_generic_function_type_aliases
    - prefer_if_elements_to_conditional_expressions
    - prefer_if_null_operators
    - prefer_initializing_formals
    - prefer_inlined_adds
    # - prefer_int_literals # conflicts with https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#use-double-literals-for-double-constants
    # - prefer_interpolation_to_compose_strings # doesn't work with raw strings, see https://github.com/dart-lang/linter/issues/2490
    - prefer_is_empty
    - prefer_is_not_empty
    - prefer_is_not_operator
    - prefer_iterable_whereType
    # - prefer_mixin # https://github.com/dart-lang/language/issues/32
    - prefer_null_aware_operators
    # - prefer_relative_imports # incompatible with sub-package imports
    - prefer_single_quotes
    - prefer_spread_collections
    - prefer_typing_uninitialized_variables
    - prefer_void_to_null
    - provide_deprecation_message
    # - public_member_api_docs # enabled on a case-by-case basis; see e.g. packages/analysis_options.yaml
    - recursive_getters
    - sized_box_for_whitespace
    - slash_for_doc_comments
    # - sort_child_properties_last # not yet tested
    - sort_constructors_first
    # - sort_pub_dependencies # prevents separating pinned transitive dependencies
    - sort_unnamed_constructors_first
    - test_types_in_equals
    - throw_in_finally
    - tighten_type_of_initializing_formals
    # - type_annotate_public_apis # subset of always_specify_types
    - type_init_formals
    # - unawaited_futures # too many false positives
    - unnecessary_await_in_return
    - unnecessary_brace_in_string_interps
    - unnecessary_const
    # - unnecessary_final # conflicts with prefer_final_locals
    - unnecessary_getters_setters
    # - unnecessary_lambdas # has false positives: https://github.com/dart-lang/linter/issues/498
    - unnecessary_new
    - unnecessary_null_aware_assignments
    # - unnecessary_null_checks # not yet tested
    - unnecessary_null_in_if_null_operators
    - unnecessary_nullable_for_final_variable_declarations
    - unnecessary_overrides
    - unnecessary_parenthesis
    # - unnecessary_raw_strings # not yet tested
    - unnecessary_statements
    - unnecessary_string_escapes
    - unnecessary_string_interpolations
    - unnecessary_this
    - unrelated_type_equality_checks
    # - unsafe_html # not yet tested
    - use_full_hex_values_for_flutter_colors
    - use_function_type_syntax_for_parameters
    # - use_if_null_to_convert_nulls_to_bools # not yet tested
    - use_is_even_rather_than_modulo
    - use_key_in_widget_constructors
    - use_late_for_private_fields_and_variables
    # - use_named_constants # not yet tested
    - use_raw_strings
    - use_rethrow_when_possible
    # - use_setters_to_change_properties # not yet tested
    # - use_string_buffers # has false positives: https://github.com/dart-lang/sdk/issues/34182
    # - use_to_and_as_if_applicable # has false positives, so we prefer to catch this by code-review
    - valid_regexps
    - void_checks

核心

analyzer:
  strong-mode:
    # 隐式转换
    implicit-casts: false
    # 隐式dynamic
    implicit-dynamic: false
  errors: # 错误级别的,你可以设置ignore来忽略,以及其他几个级别都可以设置
    # treat missing required parameters as a warning (not a hint)
    missing_required_param: warning
    # treat missing returns as a warning (not a hint)
    missing_return: warning
    # allow having TODOs in the code
    todo: ignore
    # allow self-reference to deprecated members (we do this because otherwise we have
    # to annotate every member in every test, assert, etc, when we deprecate something)
    deprecated_member_use_from_same_package: ignore
    # Ignore analyzer hints for updating pubspecs when using Future or
    # Stream and not importing dart:async
    # Please see https://github.com/flutter/flutter/pull/24528 for details.
    sdk_version_async_exported_from_core: ignore
    # Turned off until null-safe rollout is complete.
    unnecessary_null_comparison: ignore
  exclude: # 你可以通过下面方式将某个文件,某个文件夹移除规则限制
    - "bin/cache/**"

linter: # 这一部分就是各种提示,你可以根据自己的需求,添加或者删除
  rules:
    # ....

flutter analyze命令

在 控制台输入 flutter analyze --watch命令,会直接根据当前环境下的flutter SDK中的analysis_options.yaml文件里的规则进行验证。

解决问题

我上面写的滚动卡顿的问题就是因为没有按照规范来写,导致一些Widget、代码推断等造成额外的性能消耗,特别是在UI和渲染的消耗尤为严重。经过代码规范后,问题已经迎刃而解了。
比如一些Widget由于没有添加const导致在一定的情况下进行重新的绘制等。

结束语

本人对flutter的了解时间还短,对于一些知识还有所欠缺,如果有写的不当的地方还请留言指出,万分感谢。本人正在准备写一款基于社交的flutter APP,如伙伴们感兴趣,敬请关注。希望大家一起成长。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,490评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,581评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,830评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,957评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,974评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,754评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,464评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,847评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,995评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,137评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,819评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,482评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,023评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,149评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,409评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,086评论 2 355

推荐阅读更多精彩内容