Sleep sessions
Write a night of sleep with its stages in one call using writeSleepSession.
writeSleepSession writes a sleep session, such as a night, with its stages in one call. Each stage is a SleepSessionStage: a period of one kind of sleep, such as deep sleep.
On Android, the session is one Health Connect sleep session with its stages, title and notes. HealthKit has no sleep sessions, so on iOS the plugin saves the session as sleep analysis samples; see Stage types.
Signature
writeSleepSession 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 |
stages | List<SleepSessionStage> | No | const [] | The stages of the session. See Stage rules. |
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, and manual marks the samples as entered by the user. |
A SleepSessionStage holds one stage:
SleepSessionStage takes these parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
type | HealthDataType | Yes | The kind of sleep. See Stage types. |
startTime | DateTime | Yes | The start of the stage. It must be before endTime. |
endTime | DateTime | Yes | The end of the stage |
Stage rules
The stages need not cover the whole session. writeSleepSession checks them before anything is written, and throws an ArgumentError unless each stage:
- has one of the stage types;
- starts before it ends;
- lies inside the session;
- starts at or after the end of the stage before it.
The stages must therefore be sorted by start time and must not overlap. A stage can start exactly where the stage before it ends.
Return value
writeSleepSession 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.
Request write permission
All sleep types share one permission on each platform, so request WRITE access to one of them, for example SLEEP_ASLEEP:
On Android, declare the sleep permissions in android/app/src/main/AndroidManifest.xml:
Writing needs WRITE_SLEEP. Reading the sleep data of other apps, and interval reads, need READ_SLEEP.
Write a night
This sample writes an 8-hour night with six stages:
Stage types
On Android, each stage type maps to a Health Connect stage type. On iOS, the plugin saves in-bed samples for the session, which read as SLEEP_IN_BED, and maps each stage type to a HealthKit sleep analysis value:
| Stage type | Android | iOS |
|---|---|---|
SLEEP_AWAKE | STAGE_TYPE_AWAKE | awake, read as SLEEP_AWAKE |
SLEEP_AWAKE_IN_BED | STAGE_TYPE_AWAKE_IN_BED | awake, read as SLEEP_AWAKE |
SLEEP_ASLEEP | STAGE_TYPE_SLEEPING | asleepUnspecified, read as SLEEP_ASLEEP |
SLEEP_LIGHT | STAGE_TYPE_LIGHT | asleepCore, read as SLEEP_LIGHT |
SLEEP_DEEP | STAGE_TYPE_DEEP | asleepDeep, read as SLEEP_DEEP |
SLEEP_REM | STAGE_TYPE_REM | asleepREM, read as SLEEP_REM |
SLEEP_OUT_OF_BED | STAGE_TYPE_OUT_OF_BED | No sample. The time is left out of the in-bed samples. |
SLEEP_UNKNOWN | STAGE_TYPE_UNKNOWN | No sample. The time stays in bed. |
On Android, reads return each stage as its own type. On iOS, the in-bed samples cover the session except its SLEEP_OUT_OF_BED stages. Before iOS 16, HealthKit has no sleep stages: SLEEP_LIGHT, SLEEP_DEEP and SLEEP_REM stages are saved as asleep and read as SLEEP_ASLEEP.
Read a night back
Read the stages with getHealthDataFromTypes. The value of each data point is its duration in minutes:
On Android, SLEEP_SESSION returns the whole session. Reads do not return the title and notes. iOS has no SLEEP_SESSION: read SLEEP_IN_BED for the time in bed.
Reads return a sleep data point when it starts in the range. A stage is returned when the stage starts in the range, whichever session it belongs to. On Android, a SLEEP_SESSION is returned when the session starts in the range.
Reads of consecutive ranges therefore return each data point once. To read a whole night, read up to its end.
On Android, reads also find the stages of sessions that began up to 24 hours before startTime. The stages of one session come together, so the stages of overlapping sessions are not in one time order.
For the minutes of each sleep type per night, see Interval data.
Delete a night
To delete a session, call delete with the session's start as startTime and its end as endTime. What to delete differs per platform:
Any sleep type deletes your app's whole sleep sessions in the range, with all their stages:
delete removes only your app's samples of the type given, so delete SLEEP_IN_BED and each type that the stages read as:
For how delete treats the sleep types, see Delete sleep and headache data.
Examples
Write stages from their durations
A sleep tracker that reports how long each stage lasted can lay the stages out one after another from bedtime:
The session ends where its last stage ends.
Write a nap as one stage
On Android, a session without stages reads only as a SLEEP_SESSION, and on iOS only as SLEEP_IN_BED. To record a nap as sleep, give it one SLEEP_ASLEEP stage that spans it:
Error handling
writeSleepSession throws for a session it cannot write:
| Exception | When |
|---|---|
ArgumentError | startTime is not before endTime, a stage breaks the stage rules, or on iOS recordingMethod is active or unknown. It is thrown before anything is written. |
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 4 days or ends in the year 4001 or later. Its code is INVALID_ARGUMENT, and it is thrown before anything is saved. |
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 one sleep session with its stages, title and notes.
- Reads return the session as a
SLEEP_SESSIONdata point and each stage as a data point of its type, without the title and notes. deletewith any sleep type removes whole sessions with their stages.- Each sleep value written with
writeHealthDatais a session of its own, so write a night withwriteSleepSession.
- HealthKit has no sleep sessions: in one call, the plugin saves in-bed samples and a sample for each stage except out-of-bed and unknown stages.
- The title and notes are not stored, and there is no
SLEEP_SESSION. - Before iOS 16, light, deep and REM stages are saved and read as
SLEEP_ASLEEP. - A session can last at most 4 days.
Related
- Numeric data: write single values, such as one sleep stage
- Deleting data: remove the data your app wrote
- Interval data: minutes of sleep per interval
- Data types: the sleep types on each platform