tryFromJson static method

Annotation? tryFromJson(
  1. Map<String, dynamic> json, [
  2. Map<String, dynamic>? attachments
])

Defensive variant of Annotation.fromJson for the bindings/typed layer.

Returns null instead of throwing when the annotation type is unknown or the payload can't be parsed, so one malformed annotation never fails a whole page's parse. This covers the platform fallbacks the deep-dive surfaced: the Android/iOS detach fallback (pspdfkit/unknown), Web-only types not in the model (pspdfkit/comment), and empty/absent types.

Implementation

static Annotation? tryFromJson(Map<String, dynamic> json,
    [Map<String, dynamic>? attachments]) {
  final typeStr = json['type'] as String?;
  if (typeStr == null ||
      typeStr.isEmpty ||
      typeStr == 'pspdfkit/undefined' ||
      typeStr == 'pspdfkit/unknown') {
    return null;
  }
  try {
    return Annotation.fromJson(json, attachments);
  } catch (_) {
    return null;
  }
}