HealthKit Assistance

Custom Code

So I'm trying to get HealthKit to work in FF and it just seems impossible...

I have a build for a Custom Action that builds without error, but when I run the app in the simulator or a local device (through TestFlight) it won't ask for permissions or return the steps.

I have put in the custom code I used and the dependency has to be health: ^4.0.0 - as using the newest version fails and CoPilot can't figure it out as it always wants to use HealthFactory. The package is here: https://pub.dev/packages/health/example

Has anyone successfully implemented HealthKit and willing to share how?

What have you tried so far?

// Automatic FlutterFlow imports

import '/backend/backend.dart';

import '/flutter_flow/flutter_flow_theme.dart';

import '/flutter_flow/flutter_flow_util.dart';

import '/custom_code/actions/index.dart'; // Imports other custom actions

import '/flutter_flow/custom_functions.dart'; // Imports custom functions

import 'package:flutter/material.dart';

// Begin custom action code

// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'package:health/health.dart';

import 'package:flutter/services.dart';

Future<int> todaySteps() async {

// Define the types to get

List<HealthDataType> types = [HealthDataType.STEPS];

// Create an instance of HealthFactory

HealthFactory health = HealthFactory();

// Request authorization to access health data

bool requested = await health.requestAuthorization(types);

int totalSteps = 0;

if (requested) {

// Get today's date

DateTime now = DateTime.now();

DateTime start = DateTime(now.year, now.month, now.day);

DateTime end = now;

// Fetch the health data

try {

List<HealthDataPoint> healthData =

await health.getHealthDataFromTypes(start, end, types);

// Process the health data

for (HealthDataPoint point in healthData) {

if (point.type == HealthDataType.STEPS) {

totalSteps += (point.value as num).toInt();

}

}

} catch (e) {

print("Error retrieving health data: $e");

}

} else {

print("Authorization not granted");

}

return totalSteps;

}

Did you check FlutterFlow's Documentation for this topic?
Yes
1
4 replies