writeWorkoutData method

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

Write workout data to Apple Health

Returns true if successfully added workout data.

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.

Implementation

Future<bool> writeWorkoutData(
  HealthWorkoutActivityType activityType,
  DateTime start,
  DateTime end, {
  int? totalEnergyBurned,
  HealthDataUnit totalEnergyBurnedUnit = HealthDataUnit.KILOCALORIE,
  int? totalDistance,
  HealthDataUnit totalDistanceUnit = HealthDataUnit.METER,
}) async {
  // Check that value is on the current Platform
  if (_platformType == PlatformType.IOS && !_isOnIOS(activityType)) {
    throw HealthException(activityType,
        "Workout activity type $activityType is not supported on iOS");
  } else if (_platformType == PlatformType.ANDROID &&
      !_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,
  };
  final success = await _channel.invokeMethod('writeWorkoutData', args);
  return success ?? false;
}