跳到主要內容

prefer_void_to_null

穩定
有可用修復

不要使用 Null 型別,除非你確定不想要 void。

詳細資訊

#

不要 在 void 可以工作的地方使用 Null 型別。

不好的示例

dart
Null f() {}
Future<Null> f() {}
Stream<Null> f() {}
f(Null x) {}

好的示例

dart
void f() {}
Future<void> f() {}
Stream<void> f() {}
f(void x) {}

一些例外情況包括定義特殊函式型別

dart
Null Function(Null, Null);

以及建立安全的空字面量,可以傳遞給任何型別的 map 或 list 的只讀位置

dart
<Null>[];
<int, Null>{};

啟用

#

要啟用 prefer_void_to_null 規則,請在你的 analysis_options.yaml 檔案的 linter > rules 下新增 prefer_void_to_null

analysis_options.yaml
yaml
linter:
  rules:
    - prefer_void_to_null

如果你使用的是 YAML map 語法來配置 linter 規則,請在 linter > rules 下新增 prefer_void_to_null: true

analysis_options.yaml
yaml
linter:
  rules:
    prefer_void_to_null: true