Skip to content

Latest commit

 

History

History
48 lines (39 loc) · 679 Bytes

avoid_non_null_assertion.md

File metadata and controls

48 lines (39 loc) · 679 Bytes

avoid_non_null_assertion

AVOID using the non-null assertion operator (!).

BAD:

void foo(String? bar) {
  print(bar!.length);
}

GOOD:

void foo(String? bar) {
  if (bar == null) throw ArgumentError.notNull('bar');
  print(bar.length);
}

GOOD:

void foo(String? bar) {
  print(bar?.length);
}

GOOD:

void foo(String? bar) {
  if (bar case final bar?) {
    print(bar.length);
  }
}

Usage

To disable this lint in your project add this to your package's analysis_options.yaml file:

analyzer:
  plugins:
    - custom_lint

custom_lint:
    rules:
        - avoid_non_null_assertion: false