$checkKeys function Null safety
Helper function used in generated fromJson
code when
JsonSerializable.disallowUnrecognizedKeys
is true for an annotated type or
JsonKey.required
is true
for any annotated fields.
Should not be used directly.
Implementation
void $checkKeys(
Map map, {
List<String>? allowedKeys,
List<String>? requiredKeys,
List<String>? disallowNullValues,
}) {
if (allowedKeys != null) {
final invalidKeys =
map.keys.cast<String>().where((k) => !allowedKeys.contains(k)).toList();
if (invalidKeys.isNotEmpty) {
throw UnrecognizedKeysException(invalidKeys, map, allowedKeys);
}
}
if (requiredKeys != null) {
final missingKeys =
requiredKeys.where((k) => !map.keys.contains(k)).toList();
if (missingKeys.isNotEmpty) {
throw MissingRequiredKeysException(missingKeys, map);
}
}
if (disallowNullValues != null) {
final nullValuedKeys = map.entries
.where(
(entry) =>
disallowNullValues.contains(entry.key) && entry.value == null,
)
.map((entry) => entry.key as String)
.toList();
if (nullValuedKeys.isNotEmpty) {
throw DisallowedNullValueException(nullValuedKeys, map);
}
}
}