prefer-null-aware-operators
Same as prefer_null_aware_operators in Dart lints
- Group
- Complexity
- Path
lint/complexity/prefer-null-aware-operators- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 392 of 512
Flags x == null ? null : x.y (and the inverted x != null ? x.y : null).
Guarding a member access with a null-check ternary is what the ?.
null-aware operator does: x?.y. It is shorter and evaluates the receiver
only once. The rule fires when one branch is the null literal and the
other is a field access, index, or method call whose receiver matches the
checked operand (whitespace-insensitive).
Invalid
void f(C? a, C? b) {
var r1 = a == null ? null : a.field;
var r2 = a != null ? a.field : null;
var r3 = a == null ? null : a.method();
var r4 = null == a ? null : a.field;
var r5 = b == null ? null : b.method();
print('$r1 $r2 $r3 $r4 $r5');
}
class C {
int? field;
int method() => 0;
}void f(C? a, C? b) { var r1 = a == null ? null : a.field; var r1 = a == null ? null : a.field; var r2 = a != null ? a.field : null; var r2 = a != null ? a.field : null; var r3 = a == null ? null : a.method(); var r3 = a == null ? null : a.method(); var r4 = null == a ? null : a.field; var r4 = null == a ? null : a.field; var r5 = b == null ? null : b.method();Valid
void f(C? a, C? b) {
var r1 = a == null ? a.field : null;
var r2 = a == null ? null : b.field;
var r3 = a != null ? null : a.field;
var r4 = a == null ? 0 : a.field;
var r5 = a?.field;
var r6 = a == null ? null : a;
print('$r1 $r2 $r3 $r4 $r5 $r6');
}
class C {
int? field;
int method() => 0;
}Configure prefer-null-aware-operators
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"prefer-null-aware-operators": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/prefer-null-aware-operators: <reason>More complexity rules
- unnecessary-const
unnecessary_constrecommendedFlags a redundant
constkeyword inside an already-const context. - unnecessary-getters-setters
unnecessary_getters_settersrecommendedFlags a trivial getter/setter pair that only reads and writes a private field.
- unnecessary-lambdas
unnecessary_lambdasrecommendedFlags a closure that only forwards its parameters to a function a tear-off could name.
- unnecessary-overrides
unnecessary_overridesrecommendedFlags a member that does nothing but forward to
superwith the same arguments and resolved contract. - widget-tree-depthflutter
Limits the depth of an inline Flutter widget construction tree.
- avoid-classes-with-only-static-members
avoid_classes_with_only_static_membersFlags a class that declares only static members.
- avoid-collapsible-if
Flags an else-free
ifwhose only then-statement is another else-freeif. - avoid-complex-arithmetic-expressions
Reports arithmetic expressions containing too many terms.