Hello! I am building an app which loads data from yaml, but I’m not sure how to turn a YamlList into a List<String>, not a List<dynamic>.
I would like to check if it’s all strings, else throw an exception. If that works, it should set the type to List<String>.
List myList = yamlData["title"].toList;
try {
assert(myList.runtimeType is List<String>);
} on AssertionError {
throw Exception('error msg');
}
I think this is fine, not sure if I need growable: false on the toList. I want to be able to pass myList into a function requiring a List<String> once I know that’s true.
Not sure it helps, but you can use whereType<String> to filter non-String values. If you want to make sure all elements are of type String, you can also first get the length of your list and compare with the length after the whereType<String> was called.
List myList = yamlData["title"].toList();
final stringList = myList.whereType<String>();
if (myList.length == stringList.length) {
assert(myList.runtimeType is List<String>);
} else {
throw Exception('error msg');
}
Few notes: stringList.runtimeType is List<String> is never true.
You could also write the code like this:
extension EnsureType on Iterable<dynamic> {
List<T> ensureType<T>() {
return [
for (var (i, v) in this.indexed)
if (v is T)
v
else
throw Exception(
"item at index $i is not a $T but ${v.runtimeType}: ${v}"),
];
}
}
and then use it like this iterable.ensureType<String>()
I have to say working with YamlMap and JSON-like structures in general is not the most intuitive thing in Dart (and more generally, in strongly typed languages).