Flutter Analysis Options

分享你的项目加入analysis_options.yaml以后的截图到该文章评论中,而且修复它们,加入QQ群 181398081, 修复最多的一位将得到66.6红包一个,截止日期 2020/5/31 23:00。快动起手来吧,一块儿规范代码。😂html

前言

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

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

Style linter for Dart

说是Flutter的规范,实际上是Dart语言的代码规范(linter)。编程

官方叫它为 Style linter for Dart,咱们能够经过访问Lint Rules来查看全部的规范。json

咱们随便点开一个看看,好比 sort_constructors_first.async

  • 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();
}
复制代码

很是清晰,例子一看就能明白什么意思。可是那么多规则,咱们都须要去遵照吗?咱们须要遵照哪些呢?post

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全家桶,真香!

相关文章
相关标签/搜索