Change tracking
Sync the records added, updated or deleted in Health Connect since your last read using getChangesToken and getChanges.
getChangesToken and getChanges track the changes in Health Connect: the records that apps added, updated or deleted after a point in time. Instead of reading a range again, an app fetches only what changed, which saves Health Connect API quota. Change tracking is Android only; on iOS, both methods return null.
getChangesToken
| Parameter | Type | Required | Description |
|---|---|---|---|
types | List<HealthDataType> | Yes | Data types to track; must not be empty |
getChangesToken returns a changes token: a string that marks the present moment in the change history of types. getChanges returns the changes after that moment. On iOS, the method returns null.
Some types are tracked through the Health Connect record type that holds their data, so their changes come as data points of another type:
Type in types | Changes come as |
|---|---|
BODY_MASS_INDEX | WEIGHT and HEIGHT data points |
WORKOUT_ROUTE | WORKOUT data points |
Any sleep type, such as SLEEP_DEEP | SLEEP_SESSION data points |
BLOOD_PRESSURE_SYSTOLIC or BLOOD_PRESSURE_DIASTOLIC | A BLOOD_PRESSURE_SYSTOLIC and a BLOOD_PRESSURE_DIASTOLIC data point per record |
One token per data type
Use a separate token for each data type that the app syncs on its own, as Google's guide to syncing data with Health Connect recommends. When the user revokes the permission of one type, only the token of that type then fails.
getChanges
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
changesToken | String | Yes | — | The token from getChangesToken, or the nextChangesToken of the previous call; must not be empty |
includeSelf | bool | No | false | Whether to include the records that this app added or updated |
getChanges returns the next page of changes after changesToken, with the token for the next call. On iOS, the method returns null.
By default, getChanges leaves out the records that this app added or updated, as the app knows its own writes. Deletions always come, as Health Connect does not say which app deleted a record.
Return value
getChanges returns a HealthChangesResponse with these fields:
| Field | Type | Description |
|---|---|---|
changes | List<HealthChange> | The changes in this page, in the order Health Connect reports them |
nextChangesToken | String | The token for the next call |
hasMore | bool | Whether more changes are waiting; call getChanges again with nextChangesToken |
changesTokenExpired | bool | Whether the token can no longer be used; see Handle an expired token |
upsertedDataPoints | List<HealthDataPoint> | The data points of the added or updated records |
deletedRecordIds | List<String> | The IDs of the deleted records |
Each HealthChange in changes has these fields:
| Field | Type | Description |
|---|---|---|
type | HealthChangeType | upsert for a record that was added or updated, delete for a deleted record |
dataPoint | HealthDataPoint? | The data point of an upsert; null for a deletion |
dataType | HealthDataType? | The type of dataPoint; null for a deletion |
recordId | String? | The Health Connect ID of the deleted record; null for an upsert |
rawDataPoint | Map<String, dynamic>? | The data point of an upsert as the platform sent it; null for a deletion |
An upsert holds a data point as a read returns it, with the Health Connect ID of its record as uuid. A WORKOUT data point comes without its route, and without its totals, which are records of their own. A deletion holds only the record ID: Health Connect does not say the type of a deleted record.
Sync changes
Create the token before you read the data that the app keeps, and store it. Changes made while you read then come with the first getChanges call instead of being missed.
To sync, pass the stored token to getChanges, apply the changes and store nextChangesToken for the next sync. While hasMore is true, more changes are waiting.
One call, as in the example app, where localStore maps record IDs to data points:
An upserted data point carries the record ID as uuid, and the deletion of the record as recordId. Both find the same entry in localStore.
A record of a series type, such as heart rate, comes as one data point per sample, and a blood pressure record as two. All of them share the record's uuid. To track such types, keep all data points of a record under its ID, as synced_records.dart in the example app does.
The token moves on with every call. When a call fails, call getChanges again later with the same token.
Handle an expired token
changesTokenExpired is true when the token can no longer be used:
- Health Connect expires a token that is not used within 30 days;
- Health Connect cannot return the pending changes because they are too many;
- one change holds more than 100,000 data points.
In the last two cases, the plugin returns an expired response instead of throwing. A call with the same token would fail the same way. To keep the batches of changes small, sync often.
Create a new token, read the data since your last sync again and use the new token from then on. Do not store the nextChangesToken of an expired response.
For reading a long range again, see Data pages.
Examples
Keep a local copy in sync
This function applies the changes to a map of data points keyed by record ID, and returns the token for the next sync:
After 20 calls, the returned token keeps the place, and the next sync goes on from there. When syncChanges returns null, create a new token and read the data again. If a call throws, the stored token stays as it was, and the next sync applies some changes again, which localStore absorbs.
Error handling
| Exception | Method | When |
|---|---|---|
ArgumentError | Both | types or changesToken is empty. |
HealthException with notSupported | getChangesToken | A type is not available on Android or on this device, such as MINDFULNESS without the Health Connect feature. The plugin throws it before Health Connect is called. |
UnsupportedError | Both | Health Connect is not available on the device. |
HealthPlatformException | Both | Health Connect reports an error, for example rateLimitExceeded when the API quota is used up. |
Too many changes do not throw: getChanges returns changesTokenExpired: true instead. When the user revokes the READ permission of a type that the token tracks, the call may fail with permissionDenied. After a retryable error, call getChanges again later with the same token:
For every error code and what it means, see Error handling.
Platform differences
- Each
getChangescall counts against the Health Connect API quota. A page of more than 100,000 data points is read again with fewer changes, at one more call each time. - Health Connect expires a token that is not used within 30 days.
- Syncing in the background needs
READ_HEALTH_DATA_IN_BACKGROUND. - The app's own additions and updates come only with
includeSelf: true.
getChangesTokenandgetChangesreturnnullwithout calling HealthKit.- They do not throw, also not for an empty
typeslist or token. - To sync, read the data since your last sync again, for example with
getHealthDataPages. - Store the points with an upsert keyed by
type,uuidanddateFrom, so that points read twice are stored once.
Related
- Data pages: large ranges read page by page
- Health Connect: availability, features and background reads
- Health Connect limits: API quotas and call costs
- Error handling: exceptions and error codes