For the complete documentation index, see llms.txt. This page is also available as Markdown.

Flutter

Overview

This guide will walk you through the necessary steps to use the Terra Flutter Plugin with Android and Apple-based integrations. It covers everything from SDK initialization, user connection, permission handling, and background data updates.

The Terra Flutter Plugin supports the following integrations:

  • Samsung Health (Android)

  • Health Connect (Android)

  • Apple Health (iOS)


1. Install and Setup Terra Flutter

  1. Install the terra flutter package using flutter pub get terra_flutter_bridge

  2. Complete the following iOS and/or Android Setup

  1. In your terminal, cd to your /ios folder, and run pod install to install all the dependencies.

  2. Add Capabilities:

    1. Healthkit > Healthkit Background Delivery

    2. Background Modes > Background processing

    3. Background modes > Background fetch

  3. Add the following keys to your info.plist:

Method 1: Using XCode

  • 1) Go to the /ios directory of your project, open the .xcworkspace in XCode.

  • 2) Go to info.plist, and add the following keys and values:

Key
Value

Privacy - Health Share Usage Description

Description of how Health data is used

(Min 3 words)

Privacy - Health Records Usage Description

Description of how Health data is used

(Min 3 words)

Privacy - Health Update Usage Description

Description of how Health data is used

(Min 3 words)

Permitted background task scheduler

co.tryterra.data.post.request

Method 2: Directly editing info.plist

  • 1) In your app project, go to your /ios folder

  • 2) Go to info.plist, and add the following tags:

info.plist
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
  <string>co.tryterra.data.post.request</string>
</array>
  
<key>NSHealthClinicalHealthRecordsShareUsageDescription</key>
<string>Using TerraiOS to gather health data</string>

<key>NSHealthShareUsageDescription</key>
<string>Using TerraiOS as a mean of getting Health Data</string>

<key>NSHealthUpdateUsageDescription</key>
<string>Allow writing data to health kit</string>

Access Samsung Health

Terra has a privileged partnership with Samsung that gives you direct access to the Samsung Health SDK — no Health Connect intermediary needed. This is the recommended route for Samsung devices.

1. Apply for Samsung Health access

Submit your application through the Samsung Health partnership portal.

While waiting for approval, you can start development immediately using the Health Connect route below. Switching to Samsung direct later is just a version bump — no code changes needed.

2. Install the Samsung-tagged SDK

After approval, install the Samsung-tagged version of the Terra SDK:

  • React Native: npm install terra-react@<samsung-version> — find Samsung-tagged versions on the npm versions page

  • Flutter: Use the Samsung-tagged version of terra_flutter_bridge — find versions on pub.dev

3. Add ProGuard rule (required for release builds)

In your android/app/proguard-rules.pro, add:

Without this, R8 minification will strip Samsung SDK classes and cause runtime crashes in production.

4. Requirements

  • Samsung Health app must be installed on the device

  • Android 28 (minSDK 28) and above

  • Enable Developer Mode in Samsung Health on test devices (Settings > About Samsung Health > tap version number repeatedly)

  • No additional manifest or Gradle changes needed — the Terra SDK handles everything


Access Health Connect

1. In the Health Connect app

Give all permissions between the apps you wish to read from (e.g. Samsung Health, Google Fit, etc) & Health Connect.

2. Add Health Connect capability to your app

In your app project, go to the /android folder.

Include the permission tags under the Activity you wish to link the user to when they click the privacy policy link in the Health Connect permission screen. Here are the steps:

  1. Go to your Android App's AndroidManifest.xml

  2. Go to your Privacy Policy <activity> and include the following tags under this <activity> tag.

"Your Android manifest needs to have an Activity that displays your app's privacy policy, which is your app's rationale of the requested permissions, describing how the user's data is used and handled." — Health Connect.

Apply for Health Connect access


2. Initialize the SDK

The first step is to initialize the Terra SDK.

The initialization only needs to be done once on app start, (e.g. in your main.dart or an equivalent file), and every time the app is brought into the foreground. This ensures that the SDK is properly set up and ready to be used.

Step 1: Import the Terra Plugin

In your project, you should now be able to import function from the Terra Plugin. Here is an example:

Step 2: Initialize the Terra SDK

In order to interact with the SDK, you need to call initTerra first.

Call initTerra() with the following arguments:

  • devId: Your Developer ID provided by Terra.

  • referenceId: An ID of your choice to identify your app user.

Hereis an example:

(N.B This call is asynchronous, please ensure this is complete before using other SDK functions).


3. Connect a User

1. Call initConnection()

Import initConnection and call the function with the following arguments:

  • type: Specify the connection type — Connection.appleHealth, Connection.samsung, or Connection.healthConnect

  • token: A one-time authentication token generated from your backend server. This ensures secure communication between your app and the Terra servers.

  • customPermissions: A set of permissions that define what data you want to request (e.g., heart rate, steps). If empty, it defaults to all available permissions. See Permissions mapping for every value and what it requests on each platform.

  • schedulerOn:

    • iOS: This parameter has no effect. Background delivery is controlled by calling Terra.setUpBackgroundDelivery() in your AppDelegate — see Step 5 below.

    • Android: To allow Terra to make scheduled requests whenever the app is in the foreground.

Apple Health Kit Permission Screen: initConnection()

Apple Health only shows the permission popup once, so calling initConnection() multiple times won’t trigger the popup again unless:

a. you call initConnection with an expanded set of customPermissions

b. the app is deleted & reinstalled.

initConnection only needs to be called a single time.

Health Connect prohibits the permission popup to appear more than once for any given permission, so calling initConnection more than once will result in no action at all

The only case where it would re-appear is if:

  • you call initConnection with an expanded set of customPermissions

  • the app is deleted & reinstalled.

  • A permission that was not granted to use on release by Google has been requested by the app

Apple Health Kit Permission Screen: WebViews 🚧

  • Apple HealthKit implements the permissions popup as a WebView.

  • If your app is also based on a WebView, you will need to interrupt your WebView, call initConnection, then upon completion re-open your WebView.

To be able to call the initConnection() method, you need to pass a token as an argument.

This token is a single-use token created to ensure the authentication endpoint for creating a connection (and connecting the SDK to Terra's servers) does not get abused.

Generate the token with this endpoint POSThttps://api.tryterra.co/v2/auth/generateAuthToken . Make sure to call it with your Terra x-api-key and dev-id in the headers from your backend server. After you generate the token, provide the response to your client side using your own logic.

Go to the SDK Reference to find more details on the Generate the Mobile SDK Auth Token API Endpoint.


4. Validate the Connection

Always validate the connection before using the SDK

Check if a user_id exists right after initializing the Terra SDK to see if the connection still exists.

  1. Check if the User is Connected

    1. If the function returns a user ID, the user is still connected, 🎉 keep vibing along!

    2. If the function returns nil, the user needs to reconnect.

  2. Re-connect if Needed If the connection is lost, you can call terra.initConnection() again to re-establish the connection.

Calling terra.initConnection() when the user is already connected or just needs a reconnection will re-establish the connection if necessary, and the user flow will remain uninterrupted.

On Apple Health, note that a user_id only tells you the connection exists on our side, not that your app still holds HealthKit access. Deleting an app clears its HealthKit authorization while the Terra connection survives, so the permission screen can still appear on the next initConnection() if iOS has not yet determined permissions for the current install.


5. Background Delivery setup (iOS only)

For Apple apps, you can enable background delivery settings to allow data to be synced even when your app is not brought to the foreground.

  1. Go to your /ios folder in the Flutter project

  2. Call the function setUpBackgroundDelivery in your AppDelegate's didFinishLaunchingWithOptions function

This will ensure you get updates for the user's Apple Health data automatically sent to your destination.

iOS Background Delivery Behaviour:


6. Filtering by source app (iOS, optional)

Apple Health aggregates data from every health app on the user's device. If a user has both a cloud-based Terra connection (e.g. WHOOP, Garmin) and the same provider's companion app syncing into Apple Health, you'll receive duplicate data — once from the cloud API and once from the Apple Health SDK.

Use setIgnoredSources to tell the SDK to skip data from specific apps in HealthKit. Call this once on every app launch, after TerraFlutter.initTerra() completes. It is not persisted across app restarts. On Android, this function is a no-op.

Common bundle identifiers:

App
Bundle identifier

WHOOP

com.whoop.app

Garmin Connect

com.garmin.connect.mobile

Fitbit

com.fitbit.FitbitMobile

Oura

com.ouraring.oura

To find a specific app's bundle identifier, have the user check Settings → Health → Data Access & Devices on their iPhone.


Now you'll start receiving health data events automatically to your Data Destination (e.g. webhook)!

You can also request historical data to backfill, to verify that data exists, or as a fallback.

Check out the Flutter SDK reference for details about all the functions in the SDK.


Disconnecting a user

In order to disconnect an SDK user, you may use the same endpoint as for Web API-based integrations, called from your backend.


Check out the Flutter SDK reference for details about all the functions in the SDK


Data Sources requiring the Terra Mobile-SDK

Last updated

Was this helpful?