writeMeal method

Future<bool> writeMeal({
  1. required MealType mealType,
  2. required DateTime startTime,
  3. required DateTime endTime,
  4. double? caloriesConsumed,
  5. double? carbohydrates,
  6. double? protein,
  7. double? fatTotal,
  8. String? name,
  9. double? caffeine,
})

Saves meal record into Apple Health or Google Fit / Health Connect.

Returns true if successful, false otherwise.

Parameters:

  • mealType - the type of meal.
  • startTime - the start time when the meal was consumed. It must be equal to or earlier than endTime.
  • endTime - the end time when the meal was consumed. It must be equal to or later than startTime.
  • caloriesConsumed - total calories consumed with this meal.
  • carbohydrates - optional carbohydrates information.
  • protein - optional protein information.
  • fatTotal - optional total fat information.
  • name - optional name information about this meal.

Implementation

Future<bool> writeMeal({
  required MealType mealType,
  required DateTime startTime,
  required DateTime endTime,
  double? caloriesConsumed,
  double? carbohydrates,
  double? protein,
  double? fatTotal,
  String? name,
  double? caffeine,
}) async {
  if (startTime.isAfter(endTime)) {
    throw ArgumentError("startTime must be equal or earlier than endTime");
  }

  Map<String, dynamic> args = {
    'startTime': startTime.millisecondsSinceEpoch,
    'endTime': endTime.millisecondsSinceEpoch,
    'caloriesConsumed': caloriesConsumed,
    'carbohydrates': carbohydrates,
    'protein': protein,
    'fatTotal': fatTotal,
    'name': name,
    'caffeine': caffeine,
    'mealType': mealType.name,
  };
  bool? success = await _channel.invokeMethod('writeMeal', args);
  return success ?? false;
}