flutter_health_kit 0.0.1-dev.14 flutter_health_kit: ^0.0.1-dev.14 copied to clipboard
Apple HealthKit plugin for Flutter to reading and observing records
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_health_kit/flutter_health_kit.dart';
import 'package:flutter_health_kit/models.dart';
import 'package:flutter_health_kit/types.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _status = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String status;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
final result = await FlutterHealthKit.requestAuthorization(
read: [HKSampleTypeIdentifier.workout]);
status = result ? 'Authorized' : 'Not Authorized';
final workouts = await FlutterHealthKit.querySampleType<Workout>(HKSampleTypeIdentifier.workout);
debugPrint('Workouts: ${workouts.length}');
} on PlatformException {
status = 'Failed to get authorization.';
}
// 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;
setState(() {
_status = status;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Status: $_status\n'),
),
),
);
}
}