getByPath<T> method
Retrieves a value of type T from the model using a specified path.
The path is a list of keys and/or indices that navigate through nested ModelLess or
ModelLessArray structures.
If the path cannot be fully resolved, the method returns the provided default value.
path A list of keys and/or indices representing the navigation path.
def A default value of type T that is returned if the path cannot be resolved.
Returns the value of type T at the specified path, or the default value if the path cannot be resolved.
Implementation
T getByPath<T>(List<dynamic> path, {dynamic def}) {
try {
var key = path.removeAt(0);
if (path.isEmpty) {
return get<T>(key, def: def);
} else {
if (path[0] is int) {
var index = path.removeAt(0) as int;
var valueKey = get<dynamic>(key);
var valueIndex = valueKey.get<dynamic>(index);
if ((valueIndex is ModelLess || valueIndex is ModelLessArray) &&
path.isNotEmpty) {
return valueIndex.getByPath<dynamic>(path, def: def);
}
return valueIndex as T;
}
return get<ModelLess>(key).getByPath<T>(path, def: def);
}
} catch (e) {
Console.e(e);
if (T == String && def == null) {
return "" as T;
}
return def as T;
}
}