Mindfulness sessions
Write meditation and other mindfulness sessions using writeMindfulnessSession, with a type, title and notes on Android.
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
writeMindfulnessSession takes these parameters:
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
startTime | DateTime | Yes | — | The start of the session. It must be before endTime. |
endTime | DateTime | Yes | — | The end of the session |
sessionType | MindfulnessSessionType | No | MindfulnessSessionType.UNKNOWN | The type of the session (Android only). See Session types. |
title | String? | No | — | The title of the session (Android only) |
notes | String? | No | — | Notes about the session (Android only) |
recordingMethod | RecordingMethod | No | RecordingMethod.automatic | How 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:
On Android, declare the mindfulness permissions in android/app/src/main/AndroidManifest.xml:
Write a session
This sample writes a 10-minute breathing session that ends now:
Session types
MindfulnessSessionType holds the session types of Health Connect:
| Session type | Description |
|---|---|
UNKNOWN | A session of none of the other types, as Health Connect has no type for other sessions. This is the default. |
MEDITATION | Meditation |
BREATHING | Breathing exercises |
MUSIC | Music or soundscapes |
MOVEMENT | Stretching or other movement |
UNGUIDED | Unguided 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:
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:
Total mindful minutes per day
Read the minutes per day with getHealthIntervalDataFromTypes. Set start to midnight, so that the intervals start at midnight:
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:
| Exception | When |
|---|---|
ArgumentError | startTime is not before endTime, or on iOS recordingMethod is active or unknown. It is thrown before anything is written. |
HealthException | The device's Health Connect has no mindfulness sessions. Its errorCode is HealthErrorCode.notSupported. |
UnsupportedError | Health Connect is not available on the Android device. |
PlatformException | On 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
- Health Connect stores the session with its
sessionType,titleandnotes. - The device's Health Connect must have mindfulness sessions; check
isMindfulnessSessionAvailablefirst. - The app must declare
WRITE_MINDFULNESSto write sessions, andREAD_MINDFULNESSfor 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.
isMindfulnessSessionAvailablereturnstrue.- A session can last at most 28 days.
- Interval minutes count overlapping sessions once.
Related
- Sleep sessions: write a night of sleep with its stages
- Health Connect: check Health Connect features on Android
- Interval data: minutes per interval
- Deleting data: remove the sessions your app wrote