Is there a shorter, more elegant way to do that?
var? x = null == y ? null : foo(y!);
Something like this, maybe?
int? y = null;
double? x = y?.toDouble() ?? null;
(it should be null safe as well)
CodePudding user response:
No.
There is currently no operation in Dart which takes a value, checks if it is non-null, and if so, does something to it other than calling a method on the object.
What you can do, if you want to, is to introduce an extension like
extension CallWith<T> on T {
R pipeTo<R>(R Function(T) f) => f(this);
}
Then you can write:
y?.pipeTo(foo);
CodePudding user response:
there is the null aware operator ??
String? nullableString;
String notnullableString = nullableString ?? "this returns when nullableString is null";