介紹套件:flutter_lints
摘要
#package:flutter_lints
定義了最新的推薦程式碼檢查 (lint) 規則,鼓勵 Flutter 應用程式、套件和外掛程式採用良好的程式碼編寫實務。使用 Flutter 2.5 或更新版本透過 flutter create
建立的專案,已預設啟用最新的推薦程式碼檢查規則。在此版本之前建立的專案,可以使用本指南中的說明升級到最新版本。
內容
#在引入 package:flutter_lints
之前,Flutter 框架提供了一組定義在 analysis_options_user.yaml
中的程式碼檢查規則,這些規則由 dart 分析器使用,以在 Flutter 專案未定義自訂 analysis_options.yaml
檔案時識別程式碼問題。由於 analysis_options_user.yaml
與特定的框架版本綁定,因此很難在不破壞現有應用程式、套件和外掛程式的情況下進行更新。因此,analysis_options_user.yaml
中定義的程式碼檢查規則已嚴重過時。為了解決這個問題,建立了 package:flutter_lints
。該套件對程式碼檢查規則集進行版本控制,以便在不破壞現有專案的情況下進行更新。由於該套件基於 Dart 的 package:lints
,它也將 Flutter 專案的推薦程式碼檢查規則與 Dart 生態系統的其他部分對齊。
移轉指南
#請按照以下步驟將您的 Flutter 專案遷移到使用 package:flutter_lints
中最新的推薦程式碼檢查規則
透過在專案的根目錄中執行 flutter pub add --dev flutter_lints
,將 package:flutter_lints
加入您的專案 pubspec.yaml
的 dev_dependency 中。
在您的專案根目錄(與 pubspec.yaml
檔案相鄰)中建立一個 analysis_options.yaml
檔案,並加入以下內容
include: package:flutter_lints/flutter.yaml
新啟用的程式碼檢查規則集可能會在您的程式碼中發現一些新的問題。要找到這些問題,請在支援 Dart 的 IDE 中開啟您的專案,或在命令列中執行 flutter analyze
。您或許可以透過在專案的根目錄中執行 dart fix --apply
來自動修正一些回報的問題。
現有的自訂 analysis_options.yaml 檔案
#如果您的專案根目錄中已存在自訂的 analysis_options.yaml
檔案,請在其頂部加入 include: package:flutter_lints/flutter.yaml
以啟用 package:flutter_lints
中的程式碼檢查規則。如果您的 analysis_options.yaml
已包含 include:
指令,您必須決定是否要保留這些程式碼檢查規則,還是要將其替換為 package:flutter_lints
中的程式碼檢查規則,因為 Dart 分析器每個 analysis_options.yaml
檔案僅支援一個 include:
指令。
自訂 Lint
#可以在 analysis_options.yaml
檔案中進一步自訂為特定專案啟用的程式碼檢查規則。下面的範例檔案顯示了這一點,它是由 flutter create
為新專案產生的 analysis_options.yaml
檔案的複製品。
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev.org.tw/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.
# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml
linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at
# https://dart-lang.github.io/linter/lints/index.html.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
# Additional information about this file can be found at
# https://dart.dev.org.tw/guides/language/analysis-options
時間軸
#在版本中引入:2.3.0-12.0.pre
在穩定版本中:2.5
參考資料
#文件
相關議題
相關 PR
除非另有說明,否則本網站上的文件反映了 Flutter 的最新穩定版本。頁面最後更新於 2024-04-04。 檢視原始碼 或 回報問題。