loadAnnotations method

Future<Map<int, List<Annotation>>?> loadAnnotations()

Load annotations from local storage

Implementation

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

    if (!await file.exists()) {
      return null;
    }

    final jsonString = await file.readAsString();
    final Map<String, dynamic> jsonData = jsonDecode(jsonString);

    final Map<int, List<Annotation>> annotations = {};

    for (final entry in jsonData.entries) {
      final pageNumber = int.parse(entry.key);
      final annotationsJson = entry.value as List;

      final pageAnnotations = annotationsJson
          .map((annotationJson) => Annotation.fromJson(annotationJson))
          .toList();

      annotations[pageNumber] = pageAnnotations;
    }

    return annotations;
  } catch (e) {
    throw Exception('Failed to load annotations: $e');
  }
}