Cumbersome to conditionally pass Value.absent() #1209
Answered
by
simolus3
fgblomqvist
asked this question in
Q&A
-
If I have an optional parameter ThingCompanion.insert(
...
foo: foo == null ? const Value.absent() : Value(startingBalance)
) Is there a better way to do this? I feel like it's a quite common scenario. |
Beta Was this translation helpful? Give feedback.
Answered by
simolus3
May 20, 2021
Replies: 1 comment 1 reply
-
Thank you for starting this discussion. I've added
Until that is released, you can use your own function for that: Value<T> valueOf<T>(T? value) {
assert(value != null || null is! T);
return value != null ? Value(value) : const Value<Never>.absent()`
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
fgblomqvist
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for starting this discussion. I've added
Value.ofNullable
which is similar, except that it checks thatT
is non-nullable as well:Until that is released, you can use your own function for that: