Skip to main content

avoid_non_null_assertion

Rule which warns about usages of bang operator ("!") as it may result in unexpected runtime exceptions.

"Bang" operator with Maps is allowed, as Dart docs recommend using it for accessing Map values that are known to be present.

Example

BAD:

Object? object;
int? number;

final int computed = 1 + number!; // LINT
object!.method(); // LINT

GOOD:

Object? object;
int? number;

if (number != null) {
final int computed = 1 + number;
}
object?.method();

// No lint on maps
final map = {'key': 'value'};
map['key']!;