lazyFold<T> method
T
lazyFold<T>(
- T whenTrue(),
- T whenFalse()
Lazily evaluates and returns the result of whenTrue if this boolean is true,
or the result of whenFalse if this boolean is false.
This is useful when the values for the true/false branches are expensive to compute and you only want to compute the one that will actually be used.
isLoggedIn.lazyFold(
() => heavyComputation(),
() => fallback(),
);
bool isDark = true;
String status = isDark.lazyFold(
() => 'dark',
() => 'light',
);
print(status); // 'dark'
Implementation
T lazyFold<T>(T Function() whenTrue, T Function() whenFalse) {
return this ? whenTrue() : whenFalse();
}