get function
Retrieves a value from a nested Map or List structure based on a given
path. If the path does not exist, it returns the provided defaultValue
.
The path is a dot-separated string indicating the sequence of keys or indices to traverse in the nested structure. For example, given the following structure:
final map = {
'user': {
'name': 'Alice',
'address': {
'city': 'Wonderland',
'zip': 12345
}
},
'posts': [
{'title': 'Hello World'},
{'title': 'Flutter is Awesome'}
]
};
The path 'user.address.city' will return 'Wonderland', and the path 'posts.1.title' will return 'Flutter is Awesome'.
If the path does not exist, the defaultValue
is returned. If no
defaultValue
is provided, null
is returned.
Example usage:
final userCity = get(map, 'user.address.city', 'Unknown'); // 'Wonderland'
final postTitle = get(map, 'posts.1.title', 'No Title'); // 'Flutter is Awesome'
final invalidPath = get(map, 'invalid.path', 'Not Found'); // 'Not Found'
-
Parameters:
-
Returns: The value at the specified path, or
defaultValue
if the path does not exist. -
Throws: No exceptions are thrown by this function. It will return
defaultValue
if the path cannot be fully resolved. -
See also:
Implementation
dynamic get(Map<String, dynamic> map, String path, [dynamic defaultValue]) {
dynamic result = map;
final pathSegments = parsePath(path);
for (var segment in pathSegments) {
if (result is Map<String, dynamic> && result.containsKey(segment)) {
result = result[segment];
} else if (segment is int && result is List && segment < result.length) {
result = result[segment];
} else {
return defaultValue;
}
}
return result ?? defaultValue;
}