分享你的項目加入analysis_options.yaml以後的截圖到該文章評論中,而且修復它們,加入QQ羣 181398081, 修復最多的一位將得到66.6紅包一個,截止日期 2020/5/31 23:00。快動起手來吧,一塊兒規範代碼。😂html
無規則不成方圓,無論什麼平臺,寫什麼代碼。每一種編程語言都有着本身的語法標準,代碼規範,而且在不斷更新改進,達到優化語言性能的目的。git
俗話說代碼不規範,維護兩行淚
,說的就是這個道理。咱們應該遵照它們,避免代碼看起來亂七八糟。github
說是Flutter的規範,實際上是Dart語言的代碼規範(linter)。編程
官方叫它爲 Style linter for Dart
,咱們能夠經過訪問Lint Rules來查看全部的規範。json
咱們隨便點開一個看看,好比 sort_constructors_first.async
sort constructor declarations before other members.編程語言
類構造應該在其餘成員以前。工具
abstract class Animation<T> {
const Animation(this.value);
double value;
void addListener(VoidCallback listener);
}
複製代碼
abstract class Visitor {
double value;
visitSomething(Something s);
Visitor();
}
複製代碼
很是清晰,例子一看就能明白什麼意思。可是那麼多規則,咱們都須要去遵照嗎?咱們須要遵照哪些呢?post
其實Flutter Team已經制定好了。對於開發者來講,你使用的是什麼分支(branch)的Flutter SDK進行開發,你就能夠直接使用當前分支下面的指定好的規則。性能
將SDK中的analysis_options.yaml複製到大家的項目下面吧,準備接受制裁吧,騷年們!
說了那麼多,咱們本身來試一下,怎麼操(受)做(虐)。把analysis_options.yaml複製到項目下面以後,應該會在VSCode下面的PROBLEMS下面/Android Studio在Dart Analysis下面看到提示。
在提示規則的連接(上圖藍色部分,鼠標放上去後會變藍色)上面 Shift + 點擊
,就會跳轉到咱們前面講的各個規則的詳細講解上面了,按照例子改正就能夠了。
在提示有問題的代碼的地方Ctrl + .
, 就會自動彈出快速修復,好比圖中爲增長const
標識。 是否是快多了,嗯,幾千的話應該也要不了多少時間。
下面是文件裏面的核心內容
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:
- "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/**"
複製代碼
在Dart代碼中,你能夠經過下面的方式去掉某些規則的限制。
你能夠用ignore:
方式去除指定位置某些規則的警告,多個規則以,
隔開。
// ignore: prefer_const_constructors
Text('save network image to photo')
複製代碼
你能夠用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全家桶,真香!