getNestedValue function
Get value from nested structure using dot notation.
Example:
final data = {'user': {'name': 'John'}};
getNestedValue(data, 'user.name'); // 'John'
Implementation
dynamic getNestedValue(dynamic data, String path) {
if (data == null) return null;
final keys = path.split('.');
dynamic current = data;
for (final key in keys) {
if (current == null) return null;
// Handle list index access
if (current is List) {
final index = int.tryParse(key);
if (index == null || index < 0 || index >= current.length) {
return null;
}
current = current[index];
}
// Handle map key access
else if (current is Map) {
current = current[key];
}
// Can't navigate further
else {
return null;
}
}
return current;
}