Skip to content

Display extension members only on nullable instances of type #4725

Description

@FMorschel

Today we can do things like:

extension NonNullBehavior on int {
  void doStuff() => print('non-null path');
}

extension NullableBehavior on int? {
  void doStuff() => print('nullable path');
}

And Dart will trigger the NonNullBehavior version if the type is non-null. But creating a single declaration on a Foo? type makes completion for both Foo and Foo? display the declared members.

I'd like a way to be able to declare extension members that handle the nullable versions of a type that don't clutter the non-nullable completion list.

Say I have:

class C({var bool x, var int y, var String z});

extension E on C? {
  bool isNullOrXIsFalse => this?.x ?? true;
  int nullAsZeroOrY => this?.y ?? 0;
  String defaultIfNullOrZ => this?.z ?? 'default';
}

Now all cases of both C and C? have those members on the completion list, but those are way longer and useless if we know the type is not null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    requestRequests to resolve a particular developer problem

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions