11import 'dart:async' ;
2- import 'dart:math' ;
32
43import 'package:flutter/material.dart' ;
54import 'package:health/health.dart' ;
@@ -32,9 +31,13 @@ class _HealthAppState extends State<HealthApp> {
3231 AppState _state = AppState .DATA_NOT_FETCHED ;
3332 int _nofSteps = 0 ;
3433
35- // define the types to get
36- final types = dataTypesAndroid;
37- // final types = [
34+ // Define the types to get.
35+ // NOTE: These are only the ones supported on Androids new API Health Connect.
36+ // Both Android's Google Fit and iOS' HealthKit have more types that we support in the enum list [HealthDataType]
37+ // Add more - like AUDIOGRAM, HEADACHE_SEVERE etc. to try them.
38+ static final types = dataTypesAndroid;
39+ // Or selected types
40+ // static final types = [
3841 // HealthDataType.WEIGHT,
3942 // HealthDataType.STEPS,
4043 // HealthDataType.HEIGHT,
@@ -43,25 +46,17 @@ class _HealthAppState extends State<HealthApp> {
4346 // HealthDataType.BLOOD_PRESSURE_DIASTOLIC,
4447 // HealthDataType.BLOOD_PRESSURE_SYSTOLIC,
4548 // // Uncomment these lines on iOS - only available on iOS
46- // HealthDataType.AUDIOGRAM
49+ // // HealthDataType.AUDIOGRAM
4750 // ];
4851
4952 // with coresponsing permissions
50- final permissions =
51- dataTypesAndroid.map ((e) => HealthDataAccess .READ_WRITE ).toList ();
52- // final permissions = [
53- // HealthDataAccess.READ_WRITE,
54- // HealthDataAccess.READ_WRITE,
55- // HealthDataAccess.READ_WRITE,
56- // HealthDataAccess.READ_WRITE,
57- // HealthDataAccess.READ_WRITE,
58- // HealthDataAccess.READ_WRITE,
59- // HealthDataAccess.READ_WRITE,
60- // HealthDataAccess.READ_WRITE,
61- // ];
53+ // READ only
54+ // final permissions = types.map((e) => HealthDataAccess.READ).toList();
55+ // Or READ and WRITE
56+ final permissions = types.map ((e) => HealthDataAccess .READ_WRITE ).toList ();
6257
6358 // create a HealthFactory for use in the app
64- HealthFactory health = HealthFactory ();
59+ HealthFactory health = HealthFactory (useHealthConnectIfAvailable : true );
6560
6661 Future authorize () async {
6762 // If we are trying to read Step Count, Workout, Sleep or other data that requires
@@ -74,7 +69,7 @@ class _HealthAppState extends State<HealthApp> {
7469
7570 // Check if we have permission
7671 bool ? hasPermissions =
77- await HealthFactory .hasPermissions (types, permissions: permissions);
72+ await health .hasPermissions (types, permissions: permissions);
7873
7974 // hasPermissions = false because the hasPermission cannot disclose if WRITE access exists.
8075 // Hence, we have to request with WRITE as well.
@@ -130,14 +125,19 @@ class _HealthAppState extends State<HealthApp> {
130125 final now = DateTime .now ();
131126 final earlier = now.subtract (Duration (minutes: 20 ));
132127
133- bool success = await health.writeHealthData (
128+ // Add data for supported types
129+ // NOTE: These are only the ones supported on Androids new API Health Connect.
130+ // Both Android's Google Fit and iOS' HealthKit have more types that we support in the enum list [HealthDataType]
131+ // Add more - like AUDIOGRAM, HEADACHE_SEVERE etc. to try them.
132+ bool success = true ;
133+ success & = await health.writeHealthData (
134134 10 , HealthDataType .BODY_FAT_PERCENTAGE , earlier, now);
135135 success & = await health.writeHealthData (
136136 1.925 , HealthDataType .HEIGHT , earlier, now);
137137 success & =
138138 await health.writeHealthData (90 , HealthDataType .WEIGHT , earlier, now);
139- success & =
140- await health. writeHealthData ( 90 , HealthDataType .STEPS , earlier, now);
139+ success & = await health. writeHealthData (
140+ 90 , HealthDataType .HEART_RATE , earlier, now);
141141 success & =
142142 await health.writeHealthData (90 , HealthDataType .STEPS , earlier, now);
143143 success & = await health.writeHealthData (
@@ -154,38 +154,13 @@ class _HealthAppState extends State<HealthApp> {
154154 1100 , HealthDataType .DISTANCE_DELTA , earlier, now);
155155 success & =
156156 await health.writeHealthData (1.8 , HealthDataType .WATER , earlier, now);
157-
158- // // Store a count of steps taken
159- // _nofSteps = Random().nextInt(10);
160- // bool success = await health.writeHealthData(
161- // _nofSteps.toDouble(), HealthDataType.STEPS, earlier, now);
162-
163- // // Store a height
164- // success &=
165- // await health.writeHealthData(1.93, HealthDataType.HEIGHT, earlier, now);
166-
167- // // Store a Blood Glucose measurement
168- // _mgdl = Random().nextInt(10) * 1.0;
169- // success &= await health.writeHealthData(
170- // _mgdl, HealthDataType.BLOOD_GLUCOSE, now, now);
171-
172- // success &= await health.writeBloodPressure(120, 90, now, now);
173-
174- // // Store a workout eg. running
175- // success &= await health.writeWorkoutData(
176- // HealthWorkoutActivityType.RUNNING,
177- // earlier,
178- // now,
179- // // The following are optional parameters
180- // // and the UNITS are functional on iOS ONLY!
181- // totalEnergyBurned: 230,
182- // totalEnergyBurnedUnit: HealthDataUnit.KILOCALORIE,
183- // totalDistance: 1234,
184- // totalDistanceUnit: HealthDataUnit.FOOT,
185- // );
186-
187- // success &= await health.writeHealthData(
188- // 3, HealthDataType.SLEEP_ASLEEP, now.subtract(Duration(hours: 3)), now);
157+ success & = await health.writeWorkoutData (
158+ HealthWorkoutActivityType .AMERICAN_FOOTBALL ,
159+ now.subtract (Duration (minutes: 15 )),
160+ now,
161+ totalDistance: 2430 ,
162+ totalEnergyBurned: 400 );
163+ success & = await health.writeBloodPressure (90 , 80 , earlier, now);
189164
190165 // Store an Audiogram
191166 // Uncomment these on iOS - only available on iOS
@@ -215,16 +190,10 @@ class _HealthAppState extends State<HealthApp> {
215190 final now = DateTime .now ();
216191 final earlier = now.subtract (Duration (hours: 24 ));
217192
218- bool success = false ;
219-
220- success = await health.delete (HealthDataType .STEPS , earlier, now);
221- success & = await health.delete (HealthDataType .HEIGHT , earlier, now);
222- success & = await health.delete (HealthDataType .BLOOD_GLUCOSE , earlier, now);
223- success & = await health.delete (HealthDataType .WORKOUT , earlier, now);
224- success & = await health.delete (
225- HealthDataType .BLOOD_PRESSURE_SYSTOLIC ,
226- earlier,
227- now); // on Android this deletes both systolic and diastolic measurements.
193+ bool success = true ;
194+ for (HealthDataType type in types) {
195+ success & = await health.delete (type, earlier, now);
196+ }
228197
229198 setState (() {
230199 _state = success ? AppState .DATA_DELETED : AppState .DATA_NOT_DELETED ;
0 commit comments