Skip to content

Commit b3e86cd

Browse files
committed
fix: tolerate null values when normalizing cast payload maps
deepNormalizeJson's fallback rebuilt the map via raw.entries, which throws on a CastMap<String, Object>: the call sites pass `.cast<String, Object>()` over a JSON-decoded payload that legitimately holds null fields, and reading a null through the non-nullable Object cast raises "type 'Null' is not a subtype of type 'Object'". The jsonEncode fast path above fails the same way, so a single null field aborted the whole Chat/Attachment parse and dropped the event. Rebuild key-by-key with a guarded per-value lookup so a null can't blow up the normalize.
1 parent 1c65d59 commit b3e86cd

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

lib/utils/deep_map_normalize.dart

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,23 @@ dynamic deepNormalizeJson(dynamic raw) {
1717
return jsonDecode(jsonEncode(raw));
1818
} catch (_) {
1919
if (raw is Map) {
20-
return Map<String, dynamic>.fromEntries(
21-
raw.entries.map(
22-
(e) => MapEntry(e.key.toString(), deepNormalizeJson(e.value)),
23-
),
24-
);
20+
// Rebuild key-by-key. `raw` may be a CastMap<String, Object> (produced by
21+
// `.cast<String, Object>()` on a JSON-decoded socket/method-channel payload):
22+
// iterating its entries — or the jsonEncode above — casts each value to the
23+
// non-nullable Object, which throws on a genuinely-null field before we ever
24+
// reach the recursive normalize. Read each value through a guarded lookup so
25+
// a single null can't abort the whole parse (dropping the chat/attachment).
26+
final result = <String, dynamic>{};
27+
for (final key in raw.keys) {
28+
dynamic value;
29+
try {
30+
value = raw[key];
31+
} catch (_) {
32+
value = null;
33+
}
34+
result[key.toString()] = deepNormalizeJson(value);
35+
}
36+
return result;
2537
}
2638
if (raw is List) {
2739
return raw.map(deepNormalizeJson).toList();

0 commit comments

Comments
 (0)