目次

null_closures

クロージャが期待される引数にnullを渡さないでください。

このルールはDart 2.0から利用可能です。

ルールセット:推奨Flutter

このルールにはクイックフィックスが用意されています。

詳細

#

ダメ:クロージャが期待される引数にnullを渡さないでください。

メソッドに渡されるクロージャは、条件付きでしか呼び出されないことがよくあります。そのため、テストや「ハッピーパス」の運用呼び出しでは、nullが例外をスローすることを明らかにしません。

このルールは、次の場所でクロージャが期待される場所に渡されるnullリテラルのみを検出します。

コンストラクタ

#
  • dart:asyncから
    • Futureの0番目の位置引数
    • Future.microtaskの0番目の位置引数
    • Future.syncの0番目の位置引数
    • Timerの0番目の位置引数
    • Timer.periodicの1番目の位置引数
  • dart:coreから
    • List.generateの1番目の位置引数

静的関数

#
  • dart:asyncから
    • scheduleMicrotaskの0番目の位置引数
    • Future.doWhileの0番目の位置引数
    • Future.forEachの0番目の位置引数
    • Future.waitの命名パラメータcleanup
    • Timer.runの0番目の位置引数

インスタンスメソッド

#
  • dart:asyncから
    • Future.thenの0番目の位置引数
    • Future.completeの0番目の位置引数
  • dart:collectionから
    • Queue.removeWhereの0番目の位置引数
    • Queue.retainWhereの0番目の位置引数
    • Iterable.firstWhereの0番目の位置引数と命名パラメータorElse
    • Iterable.forEachの0番目の位置引数
    • Iterable.foldの1番目の位置引数
    • Iterable.lastWhereの0番目の位置引数と命名パラメータorElse
    • Iterable.mapの0番目の位置引数
    • Iterable.reduceの0番目の位置引数
    • Iterable.singleWhereの0番目の位置引数と命名パラメータorElse
    • Iterable.skipWhileの0番目の位置引数
    • Iterable.takeWhileの0番目の位置引数
    • Iterable.whereの0番目の位置引数
    • List.removeWhereの0番目の位置引数
    • List.retainWhereの0番目の位置引数
    • String.replaceAllMappedの1番目の位置引数
    • String.replaceFirstMappedの1番目の位置引数
    • String.splitMapJoinの命名パラメータonMatchonNonMatch

悪い例

dart
[1, 3, 5].firstWhere((e) => e.isOdd, orElse: null);

良い例

dart
[1, 3, 5].firstWhere((e) => e.isOdd, orElse: () => null);

使用方法

#

null_closuresルールを有効にするには、analysis_options.yamlファイルのlinter > rulesの下にnull_closuresを追加します。

analysis_options.yaml
yaml
linter:
  rules:
    - null_closures