matinzd/react-native-health-connect

kotlin.UninitializedPropertyAccessException: lateinit property requestPermission has not been initialized in Expo React Native

Closed this issue · 8 comments

Hi All,

I'm having lateinit property requestPermission has not been initialized for Health connect and I search for long long time to solve it but there is no solution

This is my Code whic I took from notJust.dev

import { useEffect, useState } from "react";
import { Platform } from "react-native";
import { initialize, requestPermission, readRecords } from 'react-native-health-connect';
import { TimeRangeFilter } from 'react-native-health-connect/lib/typescript/types/base.types';

export default function useHealthData(date: Date) {
const [hasPermissions, setHasPermission] = useState(false);

const [steps, setSteps] = useState(0);
const [distance, setDistance] = useState(0);
const [calories, setCalories] = useState(0);

// Android - Health Connect
const readSampleData = async () => {
// initialize the client
const isInitialized = await initialize();
if (!isInitialized) {
return;
}

//Request Permissions

await requestPermission([
  { accessType: 'read', recordType: 'Steps' },
  { accessType: 'read', recordType: 'Distance' },
  { accessType: 'read', recordType: 'FloorsClimbed' }
]);

const timeRangeFilter: TimeRangeFilter = {
  operator: 'between',
  startTime: new Date(date.setHours(0, 0, 0, 0)).toISOString(),
  endTime: new Date(date.setHours(23, 59, 59, 999)).toISOString(),
};

// Steps
const steps = await readRecords('Steps', { timeRangeFilter });
const totalSteps = steps.reduce((sum, cur) => sum + cur.count, 0);
setSteps(totalSteps);

// Distance
const distance = await readRecords('Distance', { timeRangeFilter });
const totalDistance = distance.reduce(
  (sum, cur) => sum + cur.distance.inMeters,
  0
);
setDistance(totalDistance);

// Floors climbed
const floorsClimbed = await readRecords('FloorsClimbed', {
  timeRangeFilter,
});
const totalFloors = floorsClimbed.reduce((sum, cur) => sum + cur.floors, 0);
setCalories(totalFloors);
// console.log(floorsClimbed);

};

useEffect(() => {
if (Platform.OS !== 'android') {
return;
}
readSampleData();
}, [date]);

return {steps, distance, calories};

}

and this is the app.json

{
"expo": {
"name": "MyHealthCoach",
"slug": "MyHealthCoach",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"userInterfaceStyle": "light",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"ios": {
"supportsTablet": true,
"bundleIdentifier": "com.myhealthcoach"
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#ffffff"
},
"package": "com.myhealthcoach",
"permissions": [
"android.permission.health.READ_STEPS",
"android.permission.health.READ_FLOORS_CLIMBED",
"android.permission.health.READ_DISTANCE"
]
},
"web": {
"favicon": "./assets/favicon.png"
},
"plugins": [
"react-native-health",
[
"expo-build-properties",
{
"android": {
"compileSdkVersion": 34,
"targetSdkVersion": 34,
"buildToolsVersion": "34.0.0",
"minSdkVersion": 26
}
}
]
],
"extra": {
"eas": {
"projectId": "2adf3f7e-7ab5-4e55-9e74-2dab1086c1c2"
}
}
}
}

kindly help me finish this issue

I am facing the same issue here. I haven't found any solution yet but I think it's related to the change they say you need to do on the MainActivity.kt@onCreate (the breaking change for the 2.0.0v).
I'm wondering how we can solve it without ejecting the project and needing to handle the native code. Maybe through a custom expo plugin?

-- UPDATE --

Apparently, the expo-health-connect expo plugin was created to solve this issue but in my case, it didn't work as expected, I still have the same problem. I read on another topic that may be a conflict with the expo-dev-client package. I have that package in my devDependencies, it facilitates the development and debugging of all the app outside the expoGo application, so I didn't try to remove that expo-dev-client dependency yet because I still need it for development purposes.

That should not interfere with our plugin. You can run the example app in that repo and it works without any issues even without having to remove dev client.

But can you please remove it for the sake of testing and see if that solves the issue? @jsalgueiro

for Me I'm still trying to have a good instruction to fix this issue I'm trying to check with your example also but i still have the same issue.

if you don't mind to give a full instruction of changing codes in so i can follow

Please send me a git repo with a minimal reproduction.

https://github.com/AbuHishamTareq/MyHealthCoachApp

you can check the app in this link and you can find the code in hooks folder

Hello All,

Finally I made it and it runs well for me now.

I follows the docs and red it well this time and finally i got it run.

My issue now that it didn't fetch the data as today it can fetch all datat before current date but not current date.

Thanks for all who tried to help me in this issue.

Hello All,

Finally I made it and it runs well for me now.

I follows the docs and red it well this time and finally i got it run.

My issue now that it didn't fetch the data as today it can fetch all datat before current date but not current date.

Thanks for all who tried to help me in this issue.

@AbuHishamTareq Can you share what solved your issue? I have the same problem here.

@AbuHishamTareq You can use expo-health-connect instead from now on.

https://github.com/matinzd/react-native-health-connect?tab=readme-ov-file#expo-installation

Closing this issue as resolved.