Skip to content

Commit 81a9cc4

Browse files
committed
chore: improve hive example
1 parent d6bcaa3 commit 81a9cc4

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

examples/flyer_chat/lib/hive_chat_controller.dart

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,32 @@ class HiveChatController
125125
}
126126
}
127127

128-
// ignore: unintended_html_in_doc_comment
129-
/// Efficient type conversion for Map<dynamic, dynamic> to Map<String, dynamic>
128+
/// Recursively converts a map with dynamic keys and values to a map with String keys.
129+
/// It's optimized to avoid creating new maps if no conversion is needed.
130130
Map<String, dynamic> _convertMap(dynamic map) {
131131
if (map is Map<String, dynamic>) {
132-
return map;
132+
Map<String, dynamic>? newMap;
133+
for (final entry in map.entries) {
134+
final value = entry.value;
135+
if (value is Map) {
136+
final convertedValue = _convertMap(value);
137+
if (convertedValue != value) {
138+
newMap ??= Map<String, dynamic>.of(map);
139+
newMap[entry.key] = convertedValue;
140+
}
141+
}
142+
}
143+
return newMap ?? map;
133144
}
134-
return Map<String, dynamic>.from(map);
145+
146+
final convertedMap = Map<String, dynamic>.from(map as Map);
147+
148+
for (final key in convertedMap.keys) {
149+
final value = convertedMap[key];
150+
if (value is Map) {
151+
convertedMap[key] = _convertMap(value);
152+
}
153+
}
154+
155+
return convertedMap;
135156
}

0 commit comments

Comments
 (0)