flutter_health_connect 1.0.2 flutter_health_connect: ^1.0.2 copied to clipboard
Flutter plugin for Google Health Connect integration. Health Connect gives you a simple way to store and connect the data between your health and fitness apps.
Flutter Health Connect #
Flutter plugin for Google Health Connect integration. Health Connect gives you a simple way to store and connect the data between your health and fitness apps.
Requirements #
Android #
- minSdkVersion:
26
(Recommend 28) - compileSdkVersion:
33
- This package requires Flutter
2.5.0
or higher.
How to install #
Android #
To interact with Health Connect within the app, declare the Health Connect package name in your AndroidManifest.xml
file:
<!-- Check whether Health Connect is installed or not -->
<queries>
<package android:name="com.google.android.apps.healthdata" />
</queries>
Every data type your app reads or writes needs to be declared using a permission in your manifest. For the full list of permissions and their corresponding data types, see List of data types.
To create the declaration, create an array resource in res/values/health_permissions.xml
.
<resources>
<array name="health_permissions">
<item>androidx.health.permission.HeartRate.READ</item>
<item>androidx.health.permission.HeartRate.WRITE</item>
<item>androidx.health.permission.Steps.READ</item>
<item>androidx.health.permission.Steps.WRITE</item>
</array>
</resources>
Inside your MainActivity declaration add a reference to health_permissions
and an intent filter for the Health Connect permissions action
<activity android:name=".MainActivity">
<meta-data android:name="health_permissions" android:resource="@array/health_permissions" />
<intent-filter>
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
</intent-filter>
</activity>
Health connect developer toolbox: http://goo.gle/health-connect-toolbox
Usage #
import 'package:flutter_health_connect/flutter_health_connect.dart';
Example #
import 'package:flutter/material.dart';
import 'package:flutter_health_connect/flutter_health_connect.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<HealthConnectDataType> types = [
HealthConnectDataType.ActiveCaloriesBurned,
HealthConnectDataType.BasalBodyTemperature,
HealthConnectDataType.BasalMetabolicRate,
HealthConnectDataType.BloodGlucose,
HealthConnectDataType.BloodPressure,
HealthConnectDataType.BodyFat,
HealthConnectDataType.BodyTemperature,
HealthConnectDataType.BoneMass,
HealthConnectDataType.CervicalMucus,
HealthConnectDataType.CyclingPedalingCadence,
HealthConnectDataType.Distance,
HealthConnectDataType.ElevationGained,
HealthConnectDataType.ExerciseEvent,
HealthConnectDataType.ExerciseLap,
HealthConnectDataType.ExerciseRepetitions,
HealthConnectDataType.ExerciseSession,
HealthConnectDataType.FloorsClimbed,
HealthConnectDataType.HeartRate,
HealthConnectDataType.Height,
HealthConnectDataType.HipCircumference,
HealthConnectDataType.Hydration,
HealthConnectDataType.LeanBodyMass,
HealthConnectDataType.MenstruationFlow,
HealthConnectDataType.Nutrition,
HealthConnectDataType.OvulationTest,
HealthConnectDataType.OxygenSaturation,
HealthConnectDataType.Power,
HealthConnectDataType.RespiratoryRate,
HealthConnectDataType.RestingHeartRate,
HealthConnectDataType.SexualActivity,
HealthConnectDataType.SleepSession,
HealthConnectDataType.SleepStage,
HealthConnectDataType.Speed,
HealthConnectDataType.StepsCadence,
HealthConnectDataType.Steps,
HealthConnectDataType.SwimmingStrokes,
HealthConnectDataType.TotalCaloriesBurned,
HealthConnectDataType.Vo2Max,
HealthConnectDataType.WaistCircumference,
HealthConnectDataType.Weight,
HealthConnectDataType.WheelchairPushes,
];
HealthConnectDataType getRecord = HealthConnectDataType.ActiveCaloriesBurned;
String resultText = '';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Health Connect'),
),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
ElevatedButton(
onPressed: () async {
var result = await HealthConnectFactory.isApiSupported();
resultText = 'isApiSupported: $result';
_updateResultText();
},
child: const Text('isApiSupported'),
),
ElevatedButton(
onPressed: () async {
var result = await HealthConnectFactory.isAvailable();
resultText = 'isAvailable: $result';
_updateResultText();
},
child: const Text('Check installed'),
),
ElevatedButton(
onPressed: () async {
await HealthConnectFactory.installHealthConnect();
},
child: const Text('Install Health Connect'),
),
ElevatedButton(
onPressed: () async {
await HealthConnectFactory.openHealthConnectSettings();
},
child: const Text('Open Health Connect Settings'),
),
ElevatedButton(
onPressed: () async {
var result = await HealthConnectFactory.hasPermissions(types);
resultText = 'hasPermissions: $result';
_updateResultText();
},
child: const Text('Has Permissions'),
),
ElevatedButton(
onPressed: () async {
var result =
await HealthConnectFactory.requestPermissions(types);
resultText = 'requestPermissions: $result';
_updateResultText();
},
child: const Text('Request Permissions'),
),
ElevatedButton(
onPressed: () async {
var startTime =
DateTime.now().subtract(const Duration(days: 4));
var endTime = DateTime.now();
var result = await HealthConnectFactory.getRecord(
type: getRecord, startTime: startTime, endTime: endTime);
resultText = '\ntype: $getRecord\n\n$result';
_updateResultText();
},
child: const Text('Get Record'),
),
Text(resultText),
],
),
),
);
}
void _updateResultText() {
setState(() {});
}
}