website logo
⌘K
Quickstart
User
Metrics
Webhooks
Mobile SDK
V2
React Native
Flutter
Android (Kotlin)
Swift
V1
Errors
FAQ
Docs powered by
Archbee
Mobile SDK
V2

Flutter

Spike Flutter SDK is a library on top of Apple HealthKit and Android Health Connect that

  1. helps with the extraction of data.
  2. pushes data to SpikeAPI and delivers standardized data.

Requirements

Android

  • Android 9+
AndroidManifest.xml
|
<manifest>
    <uses-permission android:name="android.permission.health.READ_NUTRITION"/>
    <uses-permission android:name="android.permission.health.READ_ACTIVE_CALORIES_BURNED"/>
    <uses-permission android:name="android.permission.health.READ_TOTAL_CALORIES_BURNED"/>
    <uses-permission android:name="android.permission.health.READ_STEPS"/>
    <uses-permission android:name="android.permission.health.READ_DISTANCE"/>
    <uses-permission android:name="android.permission.health.READ_ELEVATION_GAINED"/>
    <uses-permission android:name="android.permission.health.READ_RESTING_HEART_RATE"/>
    <uses-permission android:name="android.permission.health.READ_HEART_RATE_VARIABILITY"/>
    <uses-permission android:name="android.permission.health.READ_FLOORS_CLIMBED"/>
    <uses-permission android:name="android.permission.health.READ_BASAL_METABOLIC_RATE"/>
    <uses-permission android:name="android.permission.health.READ_SLEEP"/>
    <uses-permission android:name="android.permission.health.READ_HEART_RATE"/>
    <uses-permission android:name="android.permission.health.READ_EXERCISE"/>
    <uses-permission android:name="android.permission.health.READ_SPEED"/>
    <uses-permission android:name="android.permission.health.READ_POWER"/>
    … 
</manifest>


iOS

  • iOS 13.0+
  • Xcode 13.0+
  • Health Kit Reporter 2.1.0+
  • Background fetch 1.0.3+

Follow Flutter HealthKit setup instructions at https://pub.dev/packages/health_kit_reporter. If background tasks are of interest, configure your Flutter app according to this package guide: https://pub.dev/packages/background_fetch.

Signing & Capabilities

On iOS, go to your target's Signing & Capabilities section and add HealthKit. Allow Background fetch and Background Delivery.

Document image


On iOS, add HealthKit permissions descriptions to your Info.plist file.

info.plist
|
<key>NSHealthShareUsageDescription</key>
<string>We will use your health information to better track workouts.</string>

<key>NSHealthUpdateUsageDescription</key>
<string>We will update your health information to better track workouts.</string>

...
<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
</array>
...


Installation

The package can be found here.

Android

To add the SDK to your Android project, you have to add the following to your project's build.gradle file in the repositories block.

build.gradle
|
buildscript {
   repositories {
        // Other repositories
        maven {
            url 'https://gitlab.com/api/v4/projects/43396247/packages/maven'
        }
    }
}
allprojects {
    repositories {
        // Other repositories
        maven {
            url 'https://gitlab.com/api/v4/projects/43396247/packages/maven'
        }
    }
}


Usage

To set up the Flutter SDK, please provide the application ID, authentication token, and your customer user ID. You can find the application ID and authentication token in the Spike developer console. Personal identifiable information, such as emails, should not be part of user IDs.

Configuration

Dart
|
import 'package:spike_flutter_sdk/spike_api.dart';

/*
* 1. Requesting authorisation for all data types.
*/
await SpikeSdk.ensurePermissionsAreGranted();

const authToken = 'fa0b3803-6068-4e22-9788-eccce210d30c';
const appId = 'ea9e03f5-be45-49fb-bf11-47a88c184c3b';
const customerEndUserId = 'end_user_1';
const postbackURL = 'https://my_webhooks_url.com/';
const host = 'https://api.spikeapi.com';

/*
* 2. Initialising a connection for local data extraction.
*/
final conn = await SpikeSdk.createConnection(
  host: host,
  appId: appId,
  authToken: authToken,
  customerEndUserId: customerEndUserId,
);

/*
* 2. Or initialising a connection for data extraction and sending to webhooks.
*/
final webHookConnection = await SpikeSDK.createConnection(
  host: host,
  appId: appId,
  authToken: authToken,
  customerEndUserId: customerEndUserId,
  postbackURL: postbackURL,
);



Getting and using data

Once a connection has been created data can be retrieved using the extractData method. The below example shows how to retrieve daily steps for today which are returned as part of the activities summary.

Dart
|
// conn was created in the previous step

/*
* 3. Extracting heart data for today.
*/
final stepsData = await connection.extractData(SpikeDataType.steps);
if (stepsData.data.isNotEmpty) {
  print("Steps (Count): ${stepsData.data.length}");
  print("Steps[0]: ${stepsData.data[0].value}");
}
/*
* 3. Extracting and sending heart data for today to webhook.
*/
final heartJobDetails = await webHookConnection.extractAndPostData(SpikeDataType.heart);



Logging

Internally, the Flutter SDK supports logging on various levels to assist with troubleshooting. However, to avoid imposing additional third-party dependencies on library users, the SDK expects a concrete logging implementation to be provided externally. This can be done by implementing the SpikeEventLogger class and providing it when creating or unpacking a connection.

Below is an example of how to implement a simple console logger.

Dart
|
abstract class SpikeEventLogger {
  String getId();
  void onEvent(final SpikeEvent event);
}

class EventsLogger extends SpikeEventLogger {
  static final EventsLogger _singleton = EventsLogger._internal();

  factory EventsLogger() {
    return _singleton;
  }

  EventsLogger._internal();

  @override
  String getId() => 'total-tracker';

  @override
  Future<void> onEvent(SpikeEvent event) async {
    print('Event ${event.type}. Date: ${event.date}.');
    await EventsLogRepository().add(event);
  }
}




Background delivery

Background delivery enables asynchronous data delivery to the customer backend by means of webhooks. It enables data updates to be sent to the backend even when the application is hidden or closed. Background delivery is only supported on iOS devices at the moment.

Example

Below is an example of how to start background delivery for daily summaries.

Dart
|
// imports and permissions already done prior

/*
* 1. Initialising a webhook connection.
*/
final webHookConnection = await SpikeSdk.createConnection(
  appId: 'my_app_id'
  authToken: 'my_app_access_token',
  customerEndUserId: 'my_user_id',
  callbackUrl: 'https://example.com/data',
);

/*
* 2. Enabling background delivery.
*/
await SpikeSDK.enableBackgroundDelivery(
  connection: webHookConnection,
  types: [SpikeDataType.summary, SpikeDataType.heart],
  intervalSeconds: 30, // 0 would be for immediate delivery of the data.
);



After the background delivery is enabled, all relevant data is stored in memory, and you do not need to enable it again when the app opens again.

Did this page help you?
PREVIOUS
React Native
NEXT
Android (Kotlin)
Docs powered by
Archbee
TABLE OF CONTENTS
Requirements
Android
iOS
Installation
Android
Usage
Configuration
Getting and using data
Logging
Background delivery
Example
Docs powered by
Archbee