Skip to content

[Null Safety] Object extension methods on null #1497

Closed
@scopendo

Description

@scopendo

Prior to null safety, I used a series of extension methods on Object in order to simplify code, especially to avoid ternary operations to pass values to widget parameters, or when a bool expression might return null.

extension ObjectExtension on Object {
  /// Returns true if the current value is null.
  ///
  /// This works because Dart allows Object methods to be called on null.
  bool get isNull => this == null;

  /// Returns true if the current object is not null.
  bool get isNotNull => this != null;

  /// Returns the value of the first positional argument if the current
  /// value is null, otherwise it returns the value of the [otherwise]
  /// named parameter, or null if [otherwise] is not specified.
  T? ifNull<T>(T nullResult, {T? otherwise}) {
    return this == null ? nullResult : otherwise;
  }

  /// Returns the value of the first positional argument if the current
  /// value is not null, otherwise it returns the value of the [otherwise]
  /// named parameter, or null if [otherwise] is not specified.
  T? ifNotNull<T>(T notNullResult, {T? otherwise}) {
    return this == null ? otherwise : notNullResult;
  }

  /// True if the current object is a boolean with the value of true.
  ///
  /// This extension method is helpful for evaluating bool values
  /// that might be null.
  bool get isTrue {
    return (this is bool) && this == true;
  }

  /// False if the current object is not a boolean or does not have
  /// the value of true. See [isTrue].
  bool get isFalse {
    return !isTrue;
  }

  /// Returns the value of the first positional argument if the current
  /// value is true, otherwise it returns the value of the [otherwise]
  /// named parameter, or null if [otherwise] is not specified.
  T? ifTrue<T>(T trueResult, {T? otherwise}) {
    return isTrue ? trueResult : otherwise;
  }
}

It appears that with null safety, these object methods get the error:

The method 'xxx' can't be unconditionally invoked because the receiver can be 'null'.

Examples include:

final String? someOptionalText;

AppBar(
  bottom: someOptionalText.ifNotNull(
      PreferredSize(
        preferredSize: Size.fromHeight(120),
        child: Text(someOptionalText),
      ),
    ),
);

if (someOptionalText.isNotNull && someOptionalText.isNotEmpty) {
  // Prefer the readability of isNotNull over != null, especially with more complex checks
}

Is it intended that object extension methods should no longer be applicable to null?

Metadata

Metadata

Assignees

No one assigned

    Labels

    requestRequests to resolve a particular developer problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions