Flutter Analysis Options

6,447 阅读4分钟

前言

  • 无规则不成方圆,不管什么平台,写什么代码。每一种编程语言都有着自己的语法标准,代码规范,并且在不断更新改进,达到优化语言性能的目的。

  • 俗话说代码不规范,维护两行泪,说的就是这个道理。我们应该遵守它们,避免代码看起来乱七八糟。

Style linter for Dart

说是Flutter的规范,其实是Dart语言的代码规范(linter)。

官方叫它为 Style linter for Dart,我们可以通过访问Lint Rules来查看所有的规范。

我们随便点开一个看看,比如 sort_constructors_first.

  • DO

sort constructor declarations before other members.

类构造应该在其他成员之前。

  • GOOD:
abstract class Animation<T> {
  const Animation(this.value);
  double value;
  void addListener(VoidCallback listener);
}
  • BAD:
abstract class Visitor {
  double value;
  visitSomething(Something s);
  Visitor();
}

非常清晰,例子一看就能明白什么意思。但是那么多规则,我们都需要去遵守吗?我们需要遵守哪些呢?

analysis_options.yaml

其实Flutter Team已经制定好了。对于开发者来说,你使用的是什么分支(branch)的Flutter SDK进行开发,你就可以直接使用当前分支下面的指定好的规则。

将SDK中的analysis_options.yaml复制到你们的项目下面吧,准备接受制裁吧,骚年们!

实践

说了那么多,我们自己来试一下,怎么操(受)作(虐)。把analysis_options.yaml复制到项目下面之后,应该会在VSCode下面的PROBLEMS下面/Android Studio在Dart Analysis下面看到提示。

修复

  • 根据提示修复

在提示规则的链接(上图蓝色部分,鼠标放上去后会变蓝色)上面 Shift + 点击,就会跳转到我们前面讲的各个规则的详细讲解上面了,按照例子改正就可以了。

  • 利用VSCode快速修复

在提示有问题的代码的地方Ctrl + ., 就会自动弹出快速修复,比如图中为增加const标识。 是不是快多了,嗯,几千的话应该也要不了多少时间。

analysis_options.yaml文件的分析

下面是文件里面的核心内容

analyzer:
  strong-mode:
    implicit-casts: false
    implicit-dynamic: false
  enable-experiment:
    - extension-methods       
  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
    # 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
  # exclude:
  #   - "bin/cache/**"
  #   # the following two are relative to the stocks example and the flutter package respectively
  #   # see https://github.com/dart-lang/sdk/issues/28463
  #   - "lib/i18n/messages_*.dart"
  #   - "lib/src/http/**"

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

隐式转换

  strong-mode:
    # 隐式转换
    implicit-casts: false
    # 隐式dynamic
    implicit-dynamic: false

这部分是我们提示最多的一部分,因为这个选项平时默认都是true。特别是在将json转换成dart mode的时候,int i= map['test']; 这种代码是不少见的吧。庆幸的是,JsonToDart工具已经完全支持最新的analysis_options.yaml

错误

这一部分是分在error group当中的,当然你可以通过下面的方式降低或者直接ignore。

  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
    # 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

提示

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

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

exclude

你可以通过下面方式将某个文件,某个文件夹移除规则限制。

   exclude:
     - "bin/cache/**"
     # the following two are relative to the stocks example and the flutter package respectively
     # see https://github.com/dart-lang/sdk/issues/28463
     - "lib/i18n/messages_*.dart"
     - "lib/src/http/**"

ignore

在Dart代码中,你可以通过下面的方式去掉某些规则的限制。

  • ignore指定位置的某些规则

你可以用ignore:方式去除指定位置某些规则的警告,多个规则以,隔开。

  // ignore: prefer_const_constructors
  Text('save network image to photo')
  • ignore整个文件的某些规则

你可以用ignore_for_file:方式去除整个文件中某些规则的警告,多个规则以,隔开。

  // ignore_for_file: prefer_const_constructors
  Text('save network image to photo')

扩展方法

开启扩展方法,加入下面设置,并且保证Dart SDK 大于等于 2.6.0

  enable-experiment:
    - extension-methods   
environment:
  sdk: ">=2.6.0 <3.0.0"

flutter analyze 命令

我们除了在VSCode/Android Studio里面可以看到提示之外,Flutter提供了专门的analyze命令。

在终端中执行flutter analyze -h,可以看到下面提示。可以看到大部分命令默认是开启的。

接下来我们在终端中输入flutter analyze --watch ,每当文件改变的时候都会重新分析。下面是一些提示和错误。

结语

其实这篇文章的来历是因为群里有个小伙伴问

怎么规范自己的Flutter代码呢?

我看到了,就丢了一个analysis_options.yaml文件到群里,群里顿时炸开锅了。

you see see you, one by one 几千的提示,几千的错误...

虽然打码了,但是如果认出来,心里知道就好了,哈哈哈哈哈。

最后欢迎加入Flutter Candies一起制造小糖果。 (QQ群:181398081)

对了,还有,性感群秘书,在线指(劝)导(退)哦!

最最后放上Flutter Candies全家桶,真香!