saveAnnotations method

Future<void> saveAnnotations(
  1. Map<int, List<Annotation>> annotations
)

Save annotations to local storage

Implementation

Future<void> saveAnnotations(Map<int, List<Annotation>> annotations) async {
  try {
    final directory = await getApplicationDocumentsDirectory();
    final file = File('${directory.path}/$_fileName');

    final Map<String, dynamic> jsonData = {};

    for (final entry in annotations.entries) {
      final pageNumber = entry.key.toString();
      final pageAnnotations = entry.value;

      jsonData[pageNumber] = pageAnnotations.map((annotation) => annotation.toJson()).toList();
    }

    final jsonString = jsonEncode(jsonData);
    await file.writeAsString(jsonString);
  } catch (e) {
    throw Exception('Failed to save annotations: $e');
  }
}