Skip to content

Add shorthand for null-aware method execution #44698

Closed
@enloc-port

Description

@enloc-port

Say a class had a nullable method, i.e. an optional callback.

class Foo {
  Foo([this.doSomething]);
  final int Function() doSomething;
}

In order to avoid an error, I have to do a null-check on the method before executing it:

void main() {
  Foo foo = Foo();
  if (foo.doSomething != null) {
    foo.doSomething();
  }
}

But it would make the code look a lot cleaner, if this were possible:

void main() {
  Foo().doSomething?(); // null-aware execution
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed-as-intendedClosed as the reported issue is expected behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions