Skip to main content

Avoid non null assertion

Rule id

avoid-non-null-assertion

Severity

Warning

Description

Warns when non null assertion operator (! or β€œbang” operator) is used for a property access or method invocation. The operator check works at runtime and it may fail and throw a runtime exception.

The rule ignores the index [] operator on the Map class because it's considered the idiomatic way to access a known-present element in a map with []! according to the docs.

Use this rule if you want to avoid possible unexpected runtime exceptions.

Example

Bad:

class Test {
String? field;

Test? object;

void method() {
field!.contains('other'); // LINT

object!.field!.contains('other'); // LINT

final map = {'key': 'value'};
map['key']!.contains('other');

object!.method(); // LINT
}
}

Good:

class Test {
String? field;

Test? object;

void method() {
field?.contains('other');

object?.field?.contains('other');

final map = {'key': 'value'};
map['key']!.contains('other');

object?.method();
}
}