fetchActiveStreakSteps method

Future<String?> fetchActiveStreakSteps(
  1. BuildContext context
)

Gets steps of active streak of user.

Implementation

Future<String?> fetchActiveStreakSteps(BuildContext context) async {
  final localizations = AppLocalizations.of(context)!;
  final userId = user.id;
  //This query must be edited when database structure is defined
  final streaksRef =
      firestore.collection('users').doc(userId).collection('streaks');

  try {
    final querySnapshot =
        await streaksRef.where('active', isEqualTo: true).limit(1).get();

    if (querySnapshot.docs.isNotEmpty) {
      final doc = querySnapshot.docs.first;
      final steps = doc.data()['steps'];
      if (steps is String) {
        return steps;
      }
    }
  } on SocketException {
    // Network error occurred, handle accordingly
    return localizations.tDashboardNoInternetConnection;
  } on FirebaseException {
    // Handle other Firestore specific exceptions
    return localizations.tDashboardDatabaseException;
  } catch (e) {
    // Catch all other errors
    return localizations.tDashboardUnexpectedError;
  }

  return "0";
}