You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
extensionObjectExtensiononObject {
/// Returns true if the current value is null. /// /// This works because Dart allows Object methods to be called on null.boolget isNull =>this==null;
/// Returns true if the current object is not null.boolget 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}) {
returnthis==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}) {
returnthis==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.boolget isTrue {
return (thisisbool) &&this==true;
}
/// False if the current object is not a boolean or does not have /// the value of true. See [isTrue].boolget 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:
finalString? 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?
The text was updated successfully, but these errors were encountered:
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.
It appears that with null safety, these object methods get the error:
Examples include:
Is it intended that object extension methods should no longer be applicable to null?
The text was updated successfully, but these errors were encountered: