writeWorkoutData method

Future<bool> writeWorkoutData({
  1. required HealthWorkoutActivityType activityType,
  2. required DateTime start,
  3. required DateTime end,
  4. int? totalEnergyBurned,
  5. HealthDataUnit totalEnergyBurnedUnit = HealthDataUnit.KILOCALORIE,
  6. int? totalDistance,
  7. HealthDataUnit totalDistanceUnit = HealthDataUnit.METER,
  8. String? title,
})

Write workout data to Apple Health or Google Fit or Google Health Connect.

Returns true if the workout data was successfully added.

Parameters:

  • activityType The type of activity performed.
  • start The start time of the workout.
  • end The end time of the workout.
  • totalEnergyBurned The total energy burned during the workout.
  • totalEnergyBurnedUnit The UNIT used to measure totalEnergyBurned ONLY FOR IOS Default value is KILOCALORIE.
  • totalDistance The total distance traveled during the workout.
  • totalDistanceUnit The UNIT used to measure totalDistance ONLY FOR IOS Default value is METER.
  • title The title of the workout. ONLY FOR HEALTH CONNECT Default value is the activityType, e.g. "STRENGTH_TRAINING".

Implementation

Future<bool> writeWorkoutData({
  required HealthWorkoutActivityType activityType,
  required DateTime start,
  required DateTime end,
  int? totalEnergyBurned,
  HealthDataUnit totalEnergyBurnedUnit = HealthDataUnit.KILOCALORIE,
  int? totalDistance,
  HealthDataUnit totalDistanceUnit = HealthDataUnit.METER,
  String? title,
}) async {
  // Check that value is on the current Platform
  if (Platform.isIOS && !_isOnIOS(activityType)) {
    throw HealthException(activityType,
        "Workout activity type $activityType is not supported on iOS");
  } else if (Platform.isAndroid && !_isOnAndroid(activityType)) {
    throw HealthException(activityType,
        "Workout activity type $activityType is not supported on Android");
  }
  final args = <String, dynamic>{
    'activityType': activityType.name,
    'startTime': start.millisecondsSinceEpoch,
    'endTime': end.millisecondsSinceEpoch,
    'totalEnergyBurned': totalEnergyBurned,
    'totalEnergyBurnedUnit': totalEnergyBurnedUnit.name,
    'totalDistance': totalDistance,
    'totalDistanceUnit': totalDistanceUnit.name,
    'title': title,
  };
  return await _channel.invokeMethod('writeWorkoutData', args) == true;
}