flutter_health 1.2.5

  • Readme
  • Changelog
  • Example
  • Installing
  • 78

flutter_health #

This library combines both GoogleFit and AppleHealthKit. It support most of the values provided.

Works on from iOS 11.0. Some data types are supported from iOS 12.2.

Supports Android X

Dart package

Gitlab link

AppleHealthKit #

First Let's go through apple health kit:

Setup #

From Runner, select the Capabilities tab

  • Enable HealthKit
  • Check the Clinical Health Records box.

Then in the info.plist, Add string values for the following:

  • NSHealthUpdateUsageDescription
  • NSHealthShareUsageDescription

How to use #

Check if Apple Health is available on the device

await FlutterHealth.checkIfHealthDataAvailable()

Request authorization for the data types supported by the plugin

await FlutterHealth.requestAuthorization()
For now, you can request all of the options provided by the library

Get the samples of data types authorized by the user within the given time range

await FlutterHealth.getBodyFat(startDate, endDate)
await FlutterHealth.getHeartRate(startDate, endDate)
await FlutterHealth.getBloodPressureSys(startDate, endDate)

...

Data will be returning as a List of an object called HKHealthData. This object has the following attributes:

 double value;
 String unit;
 int dateFrom;
 int dateTo;

You can get those values with this library: #

Tested:

  • bodyFatPercentage
  • height
  • bodyMassIndex
  • waistCircumference
  • stepCount
  • basalEnergyBurned
  • activeEnergyBurned
  • heartRate
  • restingHeartRate
  • walkingHeartRateAverage
  • bodyTemperature
  • bloodPressureSystolic
  • bloodPressureDiastolic
  • oxygenSaturation
  • bloodGlucose
  • electrodermalActivity

Could not be tested without a watch:

  • highHeartRateEvent
  • lowHeartRateEvent
  • irregularHeartRhythmEvent

Full Example #

 await FlutterHealth.checkIfHealthDataAvailable();
 bool _isAuthorized = true;
 DateTime startDate = DateTime.utc(2018);
 DateTime endDate = DateTime.now();
 var _dataList = List<HKHealthData>();
 
 _isAuthorized = await FlutterHealth.requestAuthorization();
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBodyFat(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getHeight(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBodyMass(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getWaistCircumference(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getStepCount(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBasalEnergyBurned(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getActiveEnergyBurned(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getHeartRate(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getRestingHeartRate(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getWalkingHeartRate(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBodyTemperature(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBloodPressureSys(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBloodPressureDia(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBloodOxygen(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBloodGlucose(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getElectrodermalActivity(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getHKHighHeart(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getHKLowHeart(startDate, endDate));
 if (_isAuthorized) _dataList.addAll(await FlutterHealth.getHKIrregular(startDate, endDate));

#GoogleFit

Setup #

For GoogleFit, the initial setup is a bit longer, and can get frustrating. Just follow this setup.

How to use #

Request authorization for the data types supported by the plugin

await FlutterHealth.requestAuthorization()
For now, you can request all of the options provided by the library

Get the samples of data types authorized by the user within the given time range

For all data

await FlutterHealth.getGFAllData(startDate, endDate)

For specific type

await FlutterHealth.getGFBodyFat(startDate, endDate)
await FlutterHealth.getGFHeartRate(startDate, endDate)
await FlutterHealth.getGFBloodPressureSys(startDate, endDate)

...

Data will be returning as a List of an object called GFHealthData. This object has the following attributes:

 String value;
 String value2;
 String unit;
 int dateFrom;
 int dateTo;

You can get those GoogleFit values with this library: #

  • bodyFatPercentage
  • height
  • stepCount
  • calories
  • heartRate
  • bodyTemperature
  • bloodPressure
  • oxygenSaturation
  • bloodGlucose

1.2.3 #

Adding Weight

1.2.1 #

Updating google fit and auth in gradle

1.2.0 #

Provide compatibility with Gradle 5.x

1.1.9 #

Apple health kit for flutter (Flutter Health) initial release

You can use it to get any of the following

Tested:

  • bodyFatPercentage
  • height
  • bodyMassIndex
  • waistCircumference
  • stepCount
  • basalEnergyBurned
  • activeEnergyBurned
  • heartRate
  • restingHeartRate
  • walkingHeartRateAverage
  • bodyTemperature
  • bloodPressureSystolic
  • bloodPressureDiastolic
  • oxygenSaturation
  • bloodGlucose
  • electrodermalActivity

Could not be tested:

  • highHeartRateEvent
  • lowHeartRateEvent
  • irregularHeartRhythmEvent

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_health/flutter_health.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var _healthKitOutput;
  var _dataList = List<HKHealthData>();
  var str = "";
  bool _isAuthorized = true;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    /*Future.delayed(Duration(seconds: 2), () async {
      _healthKitOutput = await FlutterHealth.checkIfHealthDataAvailable();
      setState(() {});
    });*/

    DateTime startDate = DateTime.utc(2020, 03, 21);
    DateTime endDate = DateTime.now();
    Future.delayed(Duration(seconds: 2), () async {
      _isAuthorized = await FlutterHealth.requestAuthorization();
//      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getHKStepCountSummary(startDate, endDate));
//      setState(() {});
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getHKAllDataWithCombinedBP(startDate, endDate));
      setState(() {});
//      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getHKStepCount(startDate, endDate));
//      setState(() {});
//      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getHKHeartRate(startDate, endDate));
//      setState(() {});
      /* if (_isAuthorized) _dataList.addAll(await FlutterHealth.getHeight(startDate, endDate));
      setState(() {});
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBodyMass(startDate, endDate));
      setState(() {});
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getWaistCircumference(startDate, endDate));
      setState(() {});
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getStepCount(startDate, endDate));
      setState(() {});
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBasalEnergyBurned(startDate, endDate));
      setState(() {});
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getActiveEnergyBurned(startDate, endDate));
      setState(() {});
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getHeartRate(startDate, endDate));
      setState(() {});
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getRestingHeartRate(startDate, endDate));
      setState(() {});
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getWalkingHeartRate(startDate, endDate));
      setState(() {});
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBodyTemperature(startDate, endDate));
      setState(() {});
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBloodPressureSys(startDate, endDate));
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBloodPressureDia(startDate, endDate));
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBloodOxygen(startDate, endDate));
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getBloodGlucose(startDate, endDate));
      if (_isAuthorized) _dataList.addAll(await FlutterHealth.getElectrodermalActivity(startDate, endDate));
      setState(() {});*/
    });

    /*Future.delayed(Duration(seconds: 2), () async {
      _healthKitOutput = await FlutterHealth.getBloodType(context);
      setState(() {});
    });*/

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.sync),
              onPressed: () {
                initPlatformState();
              },
            )
          ],
        ),
        body: _dataList.isEmpty
            ? Text('Running on: $_healthKitOutput\n')
            : ListView.builder(
            itemCount: _dataList.length,
            itemBuilder: (_, index) =>
                ListTile(
                  title: Text(_dataList[index].value.toString() + " " + _dataList[index].value2.toString()),
                  trailing: Text(_dataList[index].unit),
                  subtitle: Text(DateTime.fromMillisecondsSinceEpoch(_dataList[index].dateFrom).toIso8601String()),
                  /*title: Text(str),
                  trailing: Text(str),
                  subtitle: Text(str),*/
                )),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_health: ^1.2.5

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_health/flutter_health.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
57
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
78
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Fix lib/flutter_health.dart. (-1 points)

Analysis of lib/flutter_health.dart reported 2 hints:

line 171 col 17: The stack trace variable 's' isn't used and can be removed.

line 178 col 9: The value of the local variable 'healthData' isn't used.

Format lib/generated/i18n.dart.

Run flutter format to format lib/generated/i18n.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test