avoid-non-null-assertion
added in: 1.6.0
warning
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.
note
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
if (object case int!) { // 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();
}
}