getMeals method

Future<List<Meal>?> getMeals({
  1. required User user,
  2. DateTime? day,
})

Fetches meals for a specific day from either the local database or the API.

  • user: The User object representing the current user.
  • day: The date for which to fetch the meals. If not provided, defaults to today.

Returns a Future<List<Meal>?> containing the meals for the specified day, or null if the request fails.

Implementation

Future<List<Meal>?> getMeals({required User user, DateTime? day}) async {
  day ??= DateTime.now();

  if (useLocalDb) {
    // Fetch meals from the local database
    return await _mealDatabaseRepo.getMealsForDay(user.ID, day.year, day.month, day.day);
  } else {
    // Fetch meals from the API
    final ApiReturn apiResponse = await _apiConnector.getMealRepo().getMeals(day: day, user: user);
    if (!apiResponse.success) {
      return null;
    }
    return List<Meal>.from(apiResponse.data.map((meal) => Meal.fromMap(meal)));
  }
}