Skip to content
CARP Docs
Ask AISystemLightDark
On this page

Mindfulness sessions

Write meditation and other mindfulness sessions using writeMindfulnessSession, with a type, title and notes on Android.

Copy MarkdownOpen inOpen in GitHubView as MarkdownOpen in Scira AIOpen in ChatGPTOpen in ClaudeOpen in Cursor

writeMindfulnessSession writes a mindfulness session, such as a meditation or a breathing exercise, from a start time to an end time. On Android, Health Connect stores the session with its type, title and notes. On iOS, HealthKit stores a mindful session, which has none of them.

Signature

Future<bool> writeMindfulnessSession({
  required DateTime startTime,
  required DateTime endTime,
  MindfulnessSessionType sessionType = MindfulnessSessionType.UNKNOWN,
  String? title,
  String? notes,
  RecordingMethod recordingMethod = RecordingMethod.automatic,
})

writeMindfulnessSession takes these parameters:

ParameterTypeRequiredDefaultDescription
startTimeDateTimeYes—The start of the session. It must be before endTime.
endTimeDateTimeYes—The end of the session
sessionTypeMindfulnessSessionTypeNoMindfulnessSessionType.UNKNOWNThe type of the session (Android only). See Session types.
titleString?No—The title of the session (Android only)
notesString?No—Notes about the session (Android only)
recordingMethodRecordingMethodNoRecordingMethod.automaticHow the session was recorded. On iOS, it must be manual or automatic.

Return value

writeMindfulnessSession returns true when the platform saved the session. It returns false when the platform did not save it, for example without WRITE permission.

On Android, it also returns false once the app has used up its Health Connect write quota. An invalid session throws instead; see Error handling.

Check availability

Health Connect added mindfulness sessions after its first release. isMindfulnessSessionAvailable returns whether the Health Connect on the device has them.

Mindfulness sessions come with Android 16 or later, and with SDK extension 15 on Android 14 and 15. On Android 13 and lower, they need a recent version of the Health Connect app. On iOS, isMindfulnessSessionAvailable returns true.

Where the device's Health Connect has no mindfulness sessions, reads and writes of MINDFULNESS throw a HealthException with HealthErrorCode.notSupported before Health Connect is called. isMindfulnessSessionAvailable also returns false when Health Connect is not available or the check fails.

hasPermissions is not a feature check. On Android 15 at SDK extension 13, the mindfulness permissions can be granted, for example with adb shell pm grant, while isMindfulnessSessionAvailable returns false. The Health Connect permission dialog does not offer them there, and requestAuthorization for only MINDFULNESS returns false at once.

For the other Health Connect features, see Health Connect.

Request write permission

Request WRITE access to MINDFULNESS:

final granted = await health.requestAuthorization(
  [HealthDataType.MINDFULNESS],
  permissions: [HealthDataAccess.WRITE],
);

On Android, declare the mindfulness permissions in android/app/src/main/AndroidManifest.xml:

android/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.health.READ_MINDFULNESS" />
    <uses-permission android:name="android.permission.health.WRITE_MINDFULNESS" />

    <!-- ... the rest of your manifest -->
</manifest>

Write a session

This sample writes a 10-minute breathing session that ends now:

final now = DateTime.now();

if (await health.isMindfulnessSessionAvailable()) {
  bool saved = await health.writeMindfulnessSession(
    startTime: now.subtract(const Duration(minutes: 10)),
    endTime: now,
    sessionType: MindfulnessSessionType.BREATHING, // Android only, as are title and notes
    title: 'Morning practice',
  );
}

Session types

MindfulnessSessionType holds the session types of Health Connect:

Session typeDescription
UNKNOWNA session of none of the other types, as Health Connect has no type for other sessions. This is the default.
MEDITATIONMeditation
BREATHINGBreathing exercises
MUSICMusic or soundscapes
MOVEMENTStretching or other movement
UNGUIDEDUnguided practice

Health Connect stores the type as its MINDFULNESS_SESSION_TYPE_* constant. HealthKit's mindful sessions have no type, so iOS does not store it.

Read sessions back

Reads return a session as a MINDFULNESS data point whose value is its duration in minutes. They do not return its type, title and notes:

final points = await health.getHealthDataFromTypes(
  types: [HealthDataType.MINDFULNESS],
  startTime: start,
  endTime: end,
);

for (final point in points) {
  final minutes = (point.value as NumericHealthValue).numericValue;
  print('${point.dateFrom}: $minutes min');
}

writeHealthData with MINDFULNESS also writes a session, of type UNKNOWN and without a title and notes.

getHealthIntervalDataFromTypes returns the minutes of mindfulness sessions in each interval, on both platforms; see Interval data. On Android, these are Health Connect's own totals, which on recent Health Connect versions count only the apps in the user's priority list. On iOS, the plugin computes them from the mindful sessions and counts overlapping sessions once.

Examples

Log a meditation from a timer

When a meditation timer ends, write the session. On Android, mark it as actively recorded, as the user started it; iOS accepts only manual and automatic:

Future<bool> logMeditation(DateTime start, DateTime end) async {
  if (!await health.isMindfulnessSessionAvailable()) return false;

  return health.writeMindfulnessSession(
    startTime: start,
    endTime: end,
    sessionType: MindfulnessSessionType.MEDITATION,
    title: 'Evening meditation',
    recordingMethod: Platform.isAndroid ? RecordingMethod.active : RecordingMethod.automatic,
  );
}

Total mindful minutes per day

Read the minutes per day with getHealthIntervalDataFromTypes. Set start to midnight, so that the intervals start at midnight:

final days = await health.getHealthIntervalDataFromTypes(
  startDate: start,
  endDate: end,
  types: [HealthDataType.MINDFULNESS],
  interval: 86400, // 1 day in seconds
);

for (final day in days) {
  final minutes = (day.value as NumericHealthValue).numericValue;
  print('${day.dateFrom}: $minutes min');
}

Days without sessions are left out. On Android, this read needs READ permission for MINDFULNESS. Each interval lasts 24 hours, so after a change to or from daylight saving time, the intervals start an hour off midnight.

Error handling

writeMindfulnessSession throws for a session it cannot write:

ExceptionWhen
ArgumentErrorstartTime is not before endTime, or on iOS recordingMethod is active or unknown. It is thrown before anything is written.
HealthExceptionThe device's Health Connect has no mindfulness sessions. Its errorCode is HealthErrorCode.notSupported.
UnsupportedErrorHealth Connect is not available on the Android device.
PlatformExceptionOn iOS, HealthKit does not accept the session's times: the session is longer than 28 days or ends in the year 4001 or later. Its code is INVALID_ARGUMENT, and it is thrown before anything is saved.

writeHealthData with MINDFULNESS checks the times differently: on Android, an endTime equal to startTime throws a PlatformException with code INVALID_ARGUMENT.

Write methods throw a plain PlatformException, not a HealthPlatformException. To catch it, import package:flutter/services.dart. For the exception types and error codes, see Error handling.

Platform differences

AndroidiOS
  • Health Connect stores the session with its sessionType, title and notes.
  • The device's Health Connect must have mindfulness sessions; check isMindfulnessSessionAvailable first.
  • The app must declare WRITE_MINDFULNESS to write sessions, and READ_MINDFULNESS for interval reads and the sessions of other apps.
  • Interval minutes are Health Connect's totals, which may leave out apps that are not in the user's priority list.
  • HealthKit stores a mindful session without a type, title or notes.
  • isMindfulnessSessionAvailable returns true.
  • A session can last at most 28 days.
  • Interval minutes count overlapping sessions once.

Quick links

Page · DocumentationCARP DocumentationPage · DocumentationIntroductionPage · DocumentationQuickstartPage · DocumentationTerminologyPage · DocumentationArchitecture