health_kit

Last updated:

0 purchases

health_kit Image
health_kit Images
Add to Cart

Description:

health kit

health_kit #
Wrapper for the iOS HealthKit and Android GoogleFit services for reading health and fitness data.
Usage #
To use this plugin, add health_kit as a dependency in your pubspec.yaml file.
Getting Started #
Android #
Enable Fitness API and obtain an OAuth 2.0 client ID.
iOS #
Step 1: Append the Info.plist with the following 2 entries
<key>NSHealthShareUsageDescription</key>
<string>We will sync your data with the Apple Health app to give you better insights</string>
<key>NSHealthUpdateUsageDescription</key>
<string>We will sync your data with the Apple Health app to give you better insights</string>
copied to clipboard
Step 2: Enable "HealthKit" inside the "Capabilities" tab.
Example #
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:health_kit/health_kit.dart';

void main() {
runApp(MyApp());
}

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

class _MyAppState extends State<MyApp> {
var total = 0.0;

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

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Health Kit'),
),
body: Container(
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextButton(
child: Text("Get Yesterday's Step count"),
onPressed: () async {
getYesterdayStep();
},
),
Text(total.toString()),
],
),
),
),
);
}

Future<bool> readPermissionsForHealthKit() async {
try {
final responses = await HealthKit.hasPermissions([DataType.STEP_COUNT]);

if (!responses) {
final value = await HealthKit.requestPermissions([DataType.STEP_COUNT]);

return value;
} else {
return true;
}
} on UnsupportedException catch (e) {
// thrown in case e.dataType is unsupported
print(e);
return false;
}
}

void getYesterdayStep() async {
var permissionsGiven = await readPermissionsForHealthKit();

if (permissionsGiven) {
var current = DateTime.now();

var dateFrom = DateTime.now().subtract(Duration(
hours: current.hour + 24,
minutes: current.minute,
seconds: current.second,
));
var dateTo = dateFrom.add(Duration(
hours: 23,
minutes: 59,
seconds: 59,
));

print('dateFrom: $dateFrom');
print('dateTo: $dateTo');

try {
var results = await HealthKit.read(
DataType.STEP_COUNT,
dateFrom: dateFrom,
dateTo: dateTo,
);
if (results != null) {
for (var result in results) {
total += result.value;
}
}
setState(() {});
print('value: $total');
} on Exception catch (ex) {
print('Exception in getYesterdayStep: $ex');
}
}
}
}

copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.