lateinit 属性 requestPermission 尚未使用 Health connect Expo React Native 初始化

问题描述 投票:0回答:1

我正在尝试使用react-native-health-connect实现步行步数计数器,我的问题是,当我尝试在初始化应用程序崩溃后授予权限并给出此错误lateinit属性requestPermission尚未初始化是否有任何解决这个问题的方法。

请帮我解决 ` 这是我使用的代码 https://www.notjust.dev/projects/step-counter/android-health-connect

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

const useHealthData = () => {
const [steps, setSteps] = useState(0);
const [flights, setFlights] = useState(0);
const [distance, setDistance] = useState(0);
const [androidPermissions, setAndroidPermissions] = useState<Permission[]>([]);

const init = async () => {
// initialize the client
const isInitialized = await initialize();

if (!isInitialized) {
console.log('Failed to initialize Health Connect');
return;
}

// request permissions
const grantedPermissions = await requestPermission([
{ accessType: 'read', recordType: 'Steps' },
]);

setAndroidPermissions(grantedPermissions);
};

useEffect(() => {
if (Platform.OS !== 'android') {
return;
}

init();
}, []);`

return {steps, flights, distance}
}

`

react-native expo healthconnect
1个回答
0
投票

react-native-health-connect 2.0.2 版本上的 expo 本机模块似乎存在问题。不过,我能够以老式的方式让它工作(我也在使用 expo SDK 50)

© www.soinside.com 2019 - 2024. All rights reserved.