[英]how to suppress 'The declaration isn't referenced' Dart Analysis warning for a file?
我的項目中有一個生成的 dart 文件,其中包含一些未使用的方法——這些未使用的方法導致 Android Studio 中的 Dart 分析服務器對每個未使用的方法發出警告。
警告如下所示: info: The declaration '<method name>' isn't referenced. (unused_element at [<app name>] lib/Models/<file name>.g.dart:<line number of method>)
info: The declaration '<method name>' isn't referenced. (unused_element at [<app name>] lib/Models/<file name>.g.dart:<line number of method>)
如何僅針對生成的文件抑制這些警告?
從這里的第 5 步開始(整篇文章非常值得一讀)
生成文件中的警告對您來說無關緊要。 生成的文件不受您的控制。 您不應該編輯它們,並且可能也不應該關心生成的代碼的外觀。 因此,與其用大量毫無意義的警告污染您的 IDE,只需禁用生成文件的 linter 就足夠了。 這可以通過向您的 analysis_options.yaml 添加一些代碼來完成。 在我們的例子中,我們將同時使用 json_serializable 和 Freezed,所以我們要添加的代碼是:
analyzer:
exclude:
# ignore warnings in files from json_serializable, built_value and most generators
- "**/*.g.dart"
# ignore warnings in files generated by Freezed specifically.
- "**/*.freezed.dart"
這是嵌套函數的普遍問題。 If you're declaring a local function within a parent function, return the same function using a return statement for the parent function.
就像下面的例子一樣,如果我不return max();
在父void MainMax()
中,我會收到錯誤The declaration '<method name>' isn't referenced
。 檢查並返回正確的嵌套函數。
void mainMax(int a, int b, int c) {
void max(){
if(a>b && a>c){
print (a);
} else if (b>a && b>c){
print (b);
} else{
print (c);
}
}
max();
}
作為一種解決方法,它可以被忽略
// ignore: unused_element
_func_dart_complains_about() {}
或者
// ignore_for_file: unused_element
...
_func_dart_complains_about() {}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.