toEncodableJSONMap function
Ensures that map
is an encodable JSON tree.
Implementation
Map toEncodableJSONMap(Map? map) {
if (map == null) return {};
if (map is Map<String, num>) return map;
if (map is Map<String, int>) return map;
if (map is Map<String, double>) return map;
if (map is Map<String, bool>) return map;
if (map is Map<String, String>) return map;
if (map.isEmpty) return <String, dynamic>{};
if (map is Map<String, List<num>>) return map;
if (map is Map<String, List<int>>) return map;
if (map is Map<String, List<double>>) return map;
if (map is Map<String, List<bool>>) return map;
if (map is Map<String, List<String>>) return map;
if (map is Map<String, Object> || map is Map<String, dynamic>) {
var values = map.values.toList();
if (isListEntriesAllOfType(values, num)) {
return map.map((key, value) => MapEntry(key, value as num));
} else if (isListEntriesAllOfType(values, int)) {
return map.map((key, value) => MapEntry(key, value as int));
} else if (isListEntriesAllOfType(values, double)) {
return map.map((key, value) => MapEntry(key, value as double));
} else if (isListEntriesAllOfType(values, bool)) {
return map.map((key, value) => MapEntry(key, value as bool));
} else if (isListEntriesAllOfType(values, String)) {
return map.map((key, value) => MapEntry(key, value as String));
}
}
return map.map((key, value) => MapEntry('$key', toEncodableJSON(value)));
}