我正在开发一个个人项目,我正在使用 flutter 开发一个应用程序(跨平台),该应用程序从 google fit (Android) 或 Apple Health 读取用户的健康数据。 我正在使用这个包,甚至与文档中的代码完全相同(我目前仅在 Android 上进行测试):
Future fetchStepData() async {
int? steps;
// get steps for today (i.e., since midnight)
final now = DateTime.now();
final midnight = DateTime(now.year, now.month, now.day);
bool requested = await health.requestAuthorization([HealthDataType.STEPS]);
if (requested) {
try {
steps = await health.getTotalStepsInInterval(midnight, now);
} catch (error) {
print("Caught exception in getTotalStepsInInterval: $error");
}
print('Total number of steps: $steps');
setState(() {
_nofSteps = (steps == null) ? 0 : steps;
_state = (steps == null) ? AppState.NO_DATA : AppState.STEPS_READY;
});
} else {
print("Authorization not granted - error in authorization");
setState(() => _state = AppState.DATA_NOT_FETCHED);
}
}
然后我用await调用这个函数,并且我还在所有Android清单文件中插入了正确的权限:
我还为该项目设置了 OAuth2 客户端 ID,并将我的 google 帐户添加为测试用户。
但是该函数将变量步骤始终设置为空?布尔变量“requested”为 true,所以看起来实际连接正在工作?
我对自己真的很失望,伙计们,我真的需要帮助 - 谢谢!
我尝试添加正确的android权限,明确请求权限,不同的时间间隔,但对我来说没有任何作用,我总是得到一个空值。
我也有类似的问题。添加具有指定范围的 Google 登录信息会有所帮助。我首先使用代码登录用户:
class LoginController extends GetxController {
final _googleSignIn = GoogleSignIn(scopes: ["https://www.googleapis.com/auth/fitness.activity.read"]);
var googleAccount = Rx<GoogleSignInAccount?>(null);
login() async {
googleAccount.value = await _googleSignIn.signIn();
}
}
然后我使用代码检索数据:
getSteps() async {
await Permission.activityRecognition.request();
HealthFactory health = HealthFactory();
// define the types to get
var types = [
HealthDataType.STEPS,
];
// requesting access to the data types before reading them
bool requested = await health.requestAuthorization(types);
var now = DateTime.now();
var steps = await health.getTotalStepsInInterval(now.subtract(Duration(days: 1)), now);
}
我正在使用上述内容,但仍未获得健康数据的许可。
还有其他方法可以得到正确的回应吗?
Future fetchData() async {
// Example: Check and request permissions
await _checkAndRequestPermissions();
await ref.read(googleLoginProvider.notifier).login();
// configure the health plugin before use.
// HealthFactory().configure();
/// Get everything from midnight until now
final DateTime startDate = DateTime(2020, 11, 07, 0, 0, 0);
final DateTime endDate = DateTime(2025, 11, 07, 23, 59, 59);
final HealthFactory health = HealthFactory();
// final HealthFactory health = HealthFactory(useHealthConnectIfAvailable: true);
/// Define the types to get.
final List<HealthDataType> types = [
HealthDataType.STEPS,
HealthDataType.HEIGHT,
// HealthDataType.WEIGHT,
// HealthDataType.BLOOD_GLUCOSE,
// HealthDataType.DISTANCE_WALKING_RUNNING,
];
setState(() => _state = AppState.FETCHING_DATA);
/// You MUST request access to the data types before reading them
final bool accessWasGranted = await health.requestAuthorization(types);
log("Authorization result: $accessWasGranted");
int steps = 0;
if (accessWasGranted) {
try {
/// Fetch new data
final List<HealthDataPoint> healthData =
await health.getHealthDataFromTypes(startDate, endDate, types);
/// Save all the new data points
_healthDataList.addAll(healthData);
} catch (e) {
log("Caught exception in getHealthDataFromTypes: $e");
}
/// Filter out duplicates
_healthDataList = HealthFactory.removeDuplicates(_healthDataList);
/// log the results
for (final x in _healthDataList) {
log("Data point: $x");
steps += x.value.round();
}
log("Steps: $steps");
/// Update the UI to display the results
setState(() {
_state =
_healthDataList.isEmpty ? AppState.NO_DATA : AppState.DATA_READY;
});
} else if (!accessWasGranted) {
log("Authorization not granted");
log("User did not grant access to health data. Please enable permissions
in the settings.");
setState(() => _state = AppState.DATA_NOT_FETCHED);
}
}
&检查请求权限是
Future<void> _checkAndRequestPermissions() async {
if (Platform.isAndroid) {
final PermissionStatus activityRecognitionStatus =
await Permission.activityRecognition.status;
final PermissionStatus bodySensorsStatus =
await Permission.sensors.status;
if (activityRecognitionStatus.isDenied || bodySensorsStatus.isDenied) {
// If permissions are denied, request them
await [
Permission.activityRecognition,
Permission.sensors,
].request();
}
}
}