warnUnreadable function

void warnUnreadable(
  1. Object model,
  2. String key,
  3. Object? raw,
  4. Type expected, {
  5. Object? error,
})

In debug builds, warns once per model type and key that raw exists but couldn't be read as expected.

Implementation

void warnUnreadable(
  Object model,
  String key,
  Object? raw,
  Type expected, {
  Object? error,
}) {
  if (!kDebugMode || !CorextraJsonModel.debugWarnings) return;
  // Missing and blank values are normal for optional fields, so they aren't worth a warning.
  if (raw == null || raw is String && raw.trim().isEmpty) return;
  final type = model.runtimeType;
  if (!_warned.add('$type.$key')) return;
  jsonModelWarningSink(() {
    final reason = error == null ? '' : ' ($error)';
    return '$type: "$key" should be $expected, but the JSON has ${_describe(raw)}$reason. It reads as null.';
  });
}