writeMeal method
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 thanendTime
.endTime
- the end time when the meal was consumed. It must be equal to or later thanstartTime
.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;
}