valueEnsureAsList<T> static method
Casts value to List<T>, converting each element if needed.
Implementation
static List<T> valueEnsureAsList<T>(Object? value) {
if (value is List<T>) return value;
try {
final valueList = value as List;
try {
if (!isDynamic<T>()) {
if (isMap<T>()) {
return valueList
.map((e) {
if (e is T) return e;
return Map.from(e);
})
.toList()
.cast<T>();
}
if (isMapStringDynamic<T>()) {
return valueList
.map((e) {
if (e is T) return e;
return Map<String, dynamic>.from(e);
})
.toList()
.cast<T>();
}
if (_isMapStringString<T>()) {
return valueList
.map((e) {
if (e is T) return e;
return Map<String, String>.from(e);
})
.toList()
.cast<T>();
}
if (_isListOfListInt<T>()) {
return value
.map((e) => valueEnsureAsList<List<int>>(e))
.toList()
.cast<T>();
}
if (_isListOfBytes<T>()) {
final result =
value.map((e) => valueEnsureAsList<BuBytes>(e)).toList();
return result.cast<T>();
}
if (_isBuBytes<T>()) {
final result =
value
.map((e) => valueEnsureAsList<int>(e))
.map((e) => e.asBytes)
.toList();
return result.cast<T>();
}
return value.map((e) => valueAs<T>(e)).toList();
}
} catch (_) {}
return List<T>.from(value);
} catch (e) {
throw CoreErrors.jsonWrongType(
"Failed to parse object as list<$T>",
details: {"error": e.toString()},
);
}
}