Skip to content

Instantly share code, notes, and snippets.

@Vamshi3130
Created January 23, 2024 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vamshi3130/3b7a6d4403f8b1a12aa3bc33d058bb77 to your computer and use it in GitHub Desktop.
Save Vamshi3130/3b7a6d4403f8b1a12aa3bc33d058bb77 to your computer and use it in GitHub Desktop.
solid-neutron-6522
void main() {
Map<String, dynamic> nestedMap = {
'key1': 'value1',
'key2': {
'key3': 'value3',
'key4': {
'key5': 'value5',
},
},
};
List<String> allKeys = getAllKeys(nestedMap);
print('All keys in the nested map: $allKeys');
}
List<String> getAllKeys(Map<String, dynamic> map) {
List<String> keys = [];
void traverse(Map<String, dynamic> currentMap, String currentPath) {
currentMap.forEach((key, value) {
String newPath = currentPath.isEmpty ? key : '$currentPath.$key';
if (value is Map<String, dynamic>) {
keys.add(key);
traverse(value, newPath);
} else {
keys.add(key);
}
});
}
traverse(map, '');
return keys;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment