fetchAndNavigateToVisualisation function

Future<void> fetchAndNavigateToVisualisation(
  1. BuildContext context
)

Helper function to fetch and handle survey data.

Implementation

Future<void> fetchAndNavigateToVisualisation(BuildContext context) async {
  // Show loading indicator.

  showDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      return const Center(
        child: CircularProgressIndicator(),
      );
    },
  );

  try {
    // Fetch survey data.

    final surveyData = await SurveyData.fetchAllSurveyData(context);

    // Close loading indicator.

    if (context.mounted) {
      Navigator.pop(context);
    }

    // Navigate to visualization page if we have data.

    if (surveyData.isNotEmpty) {
      if (context.mounted) {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => BPCombinedVisualisation(
              surveyData: surveyData,
            ),
          ),
        );
      }
    } else {
      // Show message if no data available.

      if (context.mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('No survey data available to visualize'),
            duration: Duration(seconds: 3),
          ),
        );
      }
    }
  } catch (e) {
    // Close loading indicator and show error.

    if (context.mounted) {
      Navigator.pop(context);
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('Error loading survey data: $e'),
          backgroundColor: Colors.red,
          duration: const Duration(seconds: 3),
        ),
      );
    }
  }
}