Copy
Future<bool> writeMeal({
required DateTime startTime,
DateTime? endTime,
DateTime? recordingTime,
MealType? mealType,
double? calories,
double? protein,
double? carbs,
double? fat,
double? caffeine,
String? name,
// ... many more micronutrient parameters
})
Core Parameters
| Parameter | Type | Description |
|---|---|---|
startTime | DateTime | When meal was consumed |
endTime | DateTime? | End time (optional) |
mealType | MealType? | Breakfast, lunch, dinner, snack |
name | String? | Meal name/description |
Macronutrients
| Parameter | Type | Unit | Description |
|---|---|---|---|
calories | double? | kcal | Total energy |
protein | double? | grams | Protein content |
carbs | double? | grams | Carbohydrates |
fat | double? | grams | Total fat |
fiber | double? | grams | Dietary fiber |
sugar | double? | grams | Sugars |
Basic Usage
Copy
// Simple meal with macros
await health.writeMeal(
startTime: DateTime.now(),
mealType: MealType.BREAKFAST,
name: 'Oatmeal with berries',
calories: 350.0,
protein: 12.0,
carbs: 58.0,
fat: 8.0,
);
// Lunch with detailed macros
await health.writeMeal(
startTime: DateTime.now(),
mealType: MealType.LUNCH,
name: 'Grilled chicken salad',
calories: 450.0,
protein: 35.0,
carbs: 25.0,
fat: 20.0,
fiber: 8.0,
sugar: 5.0,
);
Meal Types
Copy
enum MealType {
BREAKFAST,
LUNCH,
DINNER,
SNACK,
UNKNOWN,
}
// Usage
await health.writeMeal(
startTime: DateTime.now(),
mealType: MealType.SNACK,
name: 'Apple with peanut butter',
calories: 180.0,
);
Complete Nutrition Tracking
Copy
class MealLogger {
final Health health;
MealLogger(this.health);
Future<bool> logMeal({
required String name,
required MealType type,
DateTime? consumedAt,
double? calories,
double? protein,
double? carbs,
double? fat,
double? fiber,
double? sugar,
}) async {
return await health.writeMeal(
startTime: consumedAt ?? DateTime.now(),
mealType: type,
name: name,
calories: calories,
protein: protein,
carbs: carbs,
fat: fat,
fiber: fiber,
sugar: sugar,
);
}
Future<void> logDailyMeals() async {
final today = DateTime.now();
// Breakfast
await logMeal(
name: 'Scrambled eggs with toast',
type: MealType.BREAKFAST,
consumedAt: DateTime(today.year, today.month, today.day, 8, 0),
calories: 400,
protein: 24,
carbs: 35,
fat: 18,
);
// Lunch
await logMeal(
name: 'Chicken wrap',
type: MealType.LUNCH,
consumedAt: DateTime(today.year, today.month, today.day, 12, 30),
calories: 500,
protein: 32,
carbs: 48,
fat: 18,
);
// Dinner
await logMeal(
name: 'Salmon with vegetables',
type: MealType.DINNER,
consumedAt: DateTime(today.year, today.month, today.day, 19, 0),
calories: 550,
protein: 38,
carbs: 42,
fat: 22,
);
}
}
Micronutrients
ThewriteMeal method supports many micronutrients:
Copy
await health.writeMeal(
startTime: DateTime.now(),
name: 'Fortified cereal',
calories: 250.0,
// Vitamins
vitaminA: 500.0, // mcg
vitaminC: 60.0, // mg
vitaminD: 10.0, // mcg
vitaminE: 15.0, // mg
vitaminK: 80.0, // mcg
vitaminB1: 1.2, // mg (Thiamin)
vitaminB2: 1.3, // mg (Riboflavin)
vitaminB3: 16.0, // mg (Niacin)
vitaminB6: 1.4, // mg
vitaminB12: 2.4, // mcg
folate: 400.0, // mcg
// Minerals
calcium: 300.0, // mg
iron: 8.0, // mg
magnesium: 100.0, // mg
phosphorus: 250.0, // mg
potassium: 400.0, // mg
sodium: 200.0, // mg
zinc: 11.0, // mg
);
Specialized Nutrients
Copy
// Caffeine tracking
await health.writeMeal(
startTime: DateTime.now(),
name: 'Coffee',
mealType: MealType.BREAKFAST,
calories: 5.0,
caffeine: 95.0, // mg
);
// Water tracking
await health.writeMeal(
startTime: DateTime.now(),
name: 'Water',
water: 500.0, // ml
);
// Fat breakdown
await health.writeMeal(
startTime: DateTime.now(),
name: 'Avocado toast',
calories: 320.0,
fat: 18.0,
fatSaturated: 2.5, // Saturated fat (g)
fatMonounsaturated: 12.0, // Monounsaturated fat (g)
fatPolyunsaturated: 2.0, // Polyunsaturated fat (g)
cholesterol: 0.0, // mg
);
Recipe-Based Logging
Copy
class Recipe {
final String name;
final double servings;
final Map<String, double> nutrition;
Recipe({
required this.name,
required this.servings,
required this.nutrition,
});
Future<bool> logServing(Health health, double servingSize) async {
final multiplier = servingSize / servings;
return await health.writeMeal(
startTime: DateTime.now(),
name: name,
calories: (nutrition['calories'] ?? 0) * multiplier,
protein: (nutrition['protein'] ?? 0) * multiplier,
carbs: (nutrition['carbs'] ?? 0) * multiplier,
fat: (nutrition['fat'] ?? 0) * multiplier,
);
}
}
// Usage
final grilledChicken = Recipe(
name: 'Grilled Chicken Breast',
servings: 4.0,
nutrition: {
'calories': 660.0,
'protein': 120.0,
'carbs': 0.0,
'fat': 18.0,
},
);
await grilledChicken.logServing(health, 1.0); // Log 1 serving
Macro Calculation Helpers
Copy
class MacroCalculator {
// Standard calorie values per gram
static const double caloriesPerGramProtein = 4.0;
static const double caloriesPerGramCarbs = 4.0;
static const double caloriesPerGramFat = 9.0;
static double calculateTotalCalories({
double? protein,
double? carbs,
double? fat,
}) {
return (protein ?? 0) * caloriesPerGramProtein +
(carbs ?? 0) * caloriesPerGramCarbs +
(fat ?? 0) * caloriesPerGramFat;
}
static Map<String, double> calculateMacroPercentages({
required double protein,
required double carbs,
required double fat,
}) {
final totalCalories = calculateTotalCalories(
protein: protein,
carbs: carbs,
fat: fat,
);
if (totalCalories == 0) {
return {'protein': 0, 'carbs': 0, 'fat': 0};
}
return {
'protein': (protein * caloriesPerGramProtein / totalCalories * 100),
'carbs': (carbs * caloriesPerGramCarbs / totalCalories * 100),
'fat': (fat * caloriesPerGramFat / totalCalories * 100),
};
}
}
// Usage
final macros = MacroCalculator.calculateMacroPercentages(
protein: 30,
carbs: 40,
fat: 15,
);
print('Protein: ${macros['protein']?.toStringAsFixed(1)}%');
print('Carbs: ${macros['carbs']?.toStringAsFixed(1)}%');
print('Fat: ${macros['fat']?.toStringAsFixed(1)}%');
Meal Timing Patterns
Copy
class MealTiming {
static DateTime breakfast([DateTime? date]) {
final d = date ?? DateTime.now();
return DateTime(d.year, d.month, d.day, 8, 0);
}
static DateTime morningSnack([DateTime? date]) {
final d = date ?? DateTime.now();
return DateTime(d.year, d.month, d.day, 10, 30);
}
static DateTime lunch([DateTime? date]) {
final d = date ?? DateTime.now();
return DateTime(d.year, d.month, d.day, 12, 30);
}
static DateTime afternoonSnack([DateTime? date]) {
final d = date ?? DateTime.now();
return DateTime(d.year, d.month, d.day, 15, 30);
}
static DateTime dinner([DateTime? date]) {
final d = date ?? DateTime.now();
return DateTime(d.year, d.month, d.day, 19, 0);
}
}
// Usage
await health.writeMeal(
startTime: MealTiming.breakfast(),
mealType: MealType.BREAKFAST,
name: 'Morning oatmeal',
calories: 350,
);
Platform Differences
- iOS (HealthKit)
- Android (Health Connect)
- Stores as complete nutrition record
- All micronutrients supported
- Data appears in Apple Health app
- Can link with Apple’s Nutrition app
See Also
- Basic Writing - General health data
- Data Types - Nutrition-related types
- Basic Reading - Reading nutrition data