From 7c0dd5d8fde8f76e738f1741263e55f7f9c9dc1f Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Mon, 5 Jul 2021 15:40:44 +0200 Subject: [PATCH] fix: Broken nested accountData content maps If you call BasicEvent.fromJson the given content is copied first which recursively makes sure that the Map is from type Map. Using just the constructor doesnt have this which can lead that nested Maps in the content is InternallinkedHashMap and therefore lead to type errors. --- lib/src/database/hive_database.dart | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/src/database/hive_database.dart b/lib/src/database/hive_database.dart index ca039a03..08e788a9 100644 --- a/lib/src/database/hive_database.dart +++ b/lib/src/database/hive_database.dart @@ -1053,15 +1053,25 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } } +dynamic _castValue(dynamic value) { + if (value is Map) { + return convertToJson(value); + } + if (value is List) { + return value.map(_castValue).toList(); + } + return value; +} + +/// Hive always gives back an `_InternalLinkedHasMap`. This +/// creates a deep copy of the json and makes sure that the format is always +/// `Map`. Map convertToJson(Map map) { - final jsonMap = { - for (final entry in map.entries) ...{ - if (entry.value is Map) - '${entry.key.toString()}': convertToJson(entry.value), - if (!(entry.value is Map)) '${entry.key.toString()}': entry.value, - } - }; - return jsonMap; + final copy = Map.from(map); + for (final entry in copy.entries) { + copy[entry.key] = _castValue(entry.value); + } + return copy; } class MultiKey {