fetchPodSurveyData static method

Future<List<Map<String, dynamic>>> fetchPodSurveyData(
  1. BuildContext context
)

Fetches survey data from POD storage.

Implementation

static Future<List<Map<String, dynamic>>> fetchPodSurveyData(
    BuildContext context) async {
  List<Map<String, dynamic>> podData = [];
  try {
    // Get the directory URL for the bp folder.

    final dirUrl = await getDirUrl(bpDir);

    // Get resources in the container.

    final resources = await getResourcesInContainer(dirUrl);

    debugPrint('SubDirs: |${resources.subDirs.join('|')}|');
    debugPrint('Files  : |${resources.files.join('|')}|');

    // Process each file in the directory.

    for (var fileName in resources.files) {
      if (!fileName.endsWith('.enc.ttl')) continue;

      // Construct the full path including healthpod/data/bp.

      final filePath = '$bpDir/$fileName';

      if (!context.mounted) break;

      // Read the file content.

      final result = await readPod(
        filePath,
        context,
        const Text('Reading survey data'),
      );

      // Handle the response based on its type.

      if (result != SolidFunctionCallStatus.fail &&
          result != SolidFunctionCallStatus.notLoggedIn) {
        try {
          // The result is the JSON string directly.

          final data = json.decode(result.toString());
          podData.add(data);
        } catch (e) {
          debugPrint('Error parsing file $fileName: $e');
          debugPrint('Content: $result');
        }
      } else {
        debugPrint('Failed to read file $fileName: $result');
      }
    }
  } catch (e) {
    debugPrint('Error fetching POD survey data: $e');
    debugPrint('Error details: ${e.toString()}');
  }
  return podData;
}