getByPath method
Gets a value from a nested map using dot notation.
For example:
{
'user': {
'email': 'test@example.com',
'profile': {
'age': 30
}
}
}.getByPath('user.profile.age')
Returns 30.
The separator parameter allows customizing the separator used for
splitting the path. Defaults to ..
Returns null if the path doesn't exist or any intermediate value
is not a Map.
Implementation
dynamic getByPath(String path, {String separator = '.'}) {
assert(separator.isNotEmpty, 'Separator cannot be empty');
final keys = path.split(separator);
dynamic current = this;
for (final key in keys) {
if (current is Map && current.containsKey(key)) {
current = current[key];
} else {
return null;
}
}
return current;
}