getNestedValue method

dynamic getNestedValue(
  1. Map<String, dynamic> data,
  2. String path
)

Get nested value from a map/list using dot notation with numeric indices Supports paths like: "property_details.0.data.floor_details"

Implementation

dynamic getNestedValue(Map<String, dynamic> data, String path) {
  final parts = path.split('.');
  dynamic current = data;

  for (final rawPart in parts) {
    final isIndex = RegExp(r'^\d+?$').hasMatch(rawPart);

    if (current is Map<String, dynamic>) {
      // When current is a Map, numeric tokens are just keys like "0"
      current = current[rawPart];
    } else if (current is List) {
      // When current is a List, numeric tokens index into the list
      if (!isIndex) return null;
      final idx = int.tryParse(rawPart);
      if (idx == null || idx < 0 || idx >= current.length) return null;
      current = current[idx];
    } else {
      return null;
    }
  }

  return current;
}