fetchEarnedRewards method

Future<List<EarnedReward>> fetchEarnedRewards()

Retrieves all rewards that the current user has successfully earned or claimed.

Fetches the user's reward history, including all rewards they have redeemed using their accumulated points. Each earned reward includes the original reward details plus additional information about when it was earned and the points spent.

Returns:

  • List<EarnedReward>: List of all rewards earned by the current user

Usage:

try {
  final earnedRewards = await fetchEarnedRewards();
  if (earnedRewards.isNotEmpty) {
    print('You have earned ${earnedRewards.length} rewards!');

    for (final reward in earnedRewards) {
      print('Earned: ${reward.title}');
      print('Points spent: ${reward.points}');
      print('Earned on: ${reward.earnedOn}');
      print('Reward code: ${reward.rewardCode}');
      print('---');
    }

    // Calculate total points spent
    final totalPointsSpent = earnedRewards
      .where((reward) => reward.points != null)
      .fold<int>(0, (sum, reward) => sum + reward.points!);
    print('Total points spent: $totalPointsSpent');
  } else {
    print('No rewards earned yet. Start driving to earn points!');
  }
} catch (e) {
  print('Failed to fetch earned rewards: $e');
}

Throws:

  • Future.error("Unable to fetch earned rewards"): When retrieval fails

Earned Reward Information Includes:

  • ID: Unique identifier for the earned reward record
  • Reward ID: Reference to the original available reward
  • Title: Name of the earned reward
  • Description: Detailed reward information
  • Points: Points that were spent to earn this reward
  • Reward Code: Code for redemption tracking
  • Earned On: ISO 8601 timestamp of when the reward was claimed

Use Cases:

  • Reward history display
  • Points spending analysis
  • Redemption tracking
  • User achievement showcasing
  • Loyalty program analytics
  • Progress motivation

Prerequisites:

  • User must be logged in
  • Active internet connection required
  • User must have previously earned at least one reward to see results

Implementation

Future<List<EarnedReward>> fetchEarnedRewards() async {
  try {
    return await kruzr_comm.fetchEarnedRewards();
  } on Exception catch (e, stackTrace) {
    if (kDebugMode) {
      print("Error in fetchEarnedRewards");
      print(stackTrace);
      print(e);
    }
    return Future.error("Unable to fetch earned rewards");
  }
}