prefer-null-aware-method-calls
Same as prefer_null_aware_method_calls in Dart lints
- Group
- Style
- Path
lint/style/prefer-null-aware-method-calls- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 391 of 512
Prefers f?.call() to calling a null-checked function after a != null test.
if (f != null) f!(); and f != null ? f!() : null both invoke a nullable
function only when it is non-null; f?.call() says the same thing directly.
The rule fires when the condition is <expr> != null (null on the right), the
if has no else and its then-branch is that single call (optionally in a
block), or the conditional's else branch is null, and the call's function
is <expr>! with <expr> textually equal to the tested expression.
Parenthesized conditions, nulls, calls and callees do not match; compared
operands retain their parentheses, as in the official rule.
Invalid
class Holder {
void Function()? callback;
int Function(int)? transform;
}
void ifStatement(void Function()? f) {
if (f != null) f!();
}
void ifBlock(void Function(int)? f) {
if (f != null) {
f!(1);
}
}
void field(Holder holder) {
if (holder.callback != null) holder.callback!();
}
int? conditional(int Function(int)? f) => f != null ? f!(2) : null;
int? conditionalField(Holder h) =>
h.transform != null ? h.transform!(3) : null;
void spacing(void Function()? f) {
if (f!= null) f !();
}void ifStatement(void Function()? f) { if (f != null) f!(); if (f != null) { f!(1);void field(Holder holder) { if (holder.callback != null) holder.callback!(); int? conditional(int Function(int)? f) => f != null ? f!(2) : null;int? conditionalField(Holder h) => h.transform != null ? h.transform!(3) : null;void spacing(void Function()? f) { if (f!= null) f !();Valid
class Holder {
void Function()? callback;
}
void nullAware(void Function()? f) {
f?.call();
}
void withElse(void Function()? f) {
if (f != null) {
f!();
} else {
print('none');
}
}
void twoStatements(void Function()? f) {
if (f != null) {
f!();
print('done');
}
}
void differentTarget(void Function()? f, void Function()? g) {
if (f != null) g!();
}
void reversed(void Function()? f) {
if (null != f) f!();
}
int? notNullElse(int Function()? f) => f != null ? f!() : 0;
void method(Holder h) {
if (h.callback != null) h.callback?.call();
}Configure prefer-null-aware-method-calls
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-null-aware-method-calls": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-null-aware-method-calls: <reason>More style rules
- prefer-padding-over-containerflutter
Prefers
Paddingover aContainerused only forpadding. - prefer-provider-extensionsprovider
Prefer context.read/watch when provider extensions are in scope.
- prefer-returning-condition
Flags an
ifreturning a boolean literal followed by the opposite literal. - prefer-riverpod-notifier-suffixriverpod
Requires a
Notifiersuffix on classes extending resolved Riverpod notifiers. - prefer-riverpod-provider-suffixriverpod
Requires a Provider suffix on resolved Riverpod top-level providers and static fields.
- prefer-single-quotes
prefer_single_quotesFlags double-quoted string literals that could use single quotes without needing an escape.
- prefer-sized-box-squareflutter
Prefers SizedBox.square for an unnamed Flutter
SizedBoxwhose explicit width and height are stable variable reads or numeric literals with identical token spelling. - prefer-spread-collections
prefer_spread_collectionsrecommendedFlags an
addAllcascade on a collection literal that a spread would express.