addAnnotation method
Adds a typed annotation to the document.
Returns the created annotation parsed back from the platform, or null
if the platform did not return a parseable result.
Annotations that carry binary content mix in HasAttachment — currently
ImageAnnotation and RichMediaAnnotation. For those, the
AnnotationAttachment's Base64 binary + contentType are forwarded to
the platform so the annotation renders; the binary travels out-of-band via
addAnnotationJson's attachment argument rather than inline in the
Instant JSON. Other types (including StampAnnotation) don't carry an
attachment and are written from their Instant JSON alone.
Implementation
Future<Annotation?> addAnnotation(Annotation annotation) async {
String? attachment;
if (annotation is HasAttachment) {
final att = (annotation as HasAttachment).attachment;
if (att != null && att.binary.isNotEmpty) {
attachment = jsonEncode(att.toJson());
}
}
final created = await addAnnotationJson(
jsonEncode(annotation.toJson()),
attachment: attachment,
);
if (created.isEmpty) return null;
try {
final decoded = jsonDecode(created);
if (decoded is Map) {
return Annotation.tryFromJson(Map<String, dynamic>.from(decoded));
}
} catch (_) {}
return null;
}