letIterableOrNull<T> function
Iterable<T> ?
letIterableOrNull<T>(
- dynamic input, {
- bool filterNulls = false,
- dynamic nullFallback,
Let's you convert input
to an Iterable type if possible, or returns null
if the conversion cannot be performed.
If filterNulls
is true, the returned iterable will not contain any null
values. If nullFallback
is provided, it will be used as a fallback value
for null
values.
Implementation
Iterable<T>? letIterableOrNull<T>(
dynamic input, {
bool filterNulls = false,
dynamic nullFallback,
}) {
final nullable = isNullable<T>();
if (!nullable && input == null) return null;
dynamic decoded;
if (input is String) {
decoded = letIterableFromCsv(input);
} else {
decoded = input;
}
if (decoded is Iterable) {
try {
final a = decoded.map((e) {
final result = letOrNull<T>(e) ?? letOrNull<T>(nullFallback);
if (filterNulls) {
if (!nullable && result == null) {
return const _Empty();
}
}
return result;
});
final b = a.where((e) => e != const _Empty());
var c = b.map((e) => e as T);
if (filterNulls) {
c = c.where((e) => e != null);
}
return c;
} catch (_) {}
}
return [input as T];
}