Reputation: 11
I am currently working on a Flutter application where I'm utilizing the health package (version 8.1.0) to fetch steps and workout data from the user's device. The steps data retrieval works seamlessly, but when attempting to fetch workout data on Android, I encounter the following error: "There was an error getting the workout data! 4: The user must be signed in to make this API call."
It's important to note that this issue is specific to Android, as everything works correctly on iOS.
I have thoroughly reviewed the health package documentation and ensured that OAuth 2.0 credentials are properly configured in Google Cloud.
Code samples:
...
HealthFactory health = HealthFactory(useHealthConnectIfAvailable: false);
...
final types = [
HealthDataType.STEPS,
HealthDataType.HEART_RATE,
HealthDataType.WORKOUT,
];
final permissions = [
HealthDataAccess.READ,
HealthDataAccess.READ,
HealthDataAccess.READ,
];
...
PermissionStatus hasGoogleActivityRecognition =
await Permission.activityRecognition.request();
PermissionStatus hasGoogleLocationPermission =
await Permission.location.request();
if (hasGoogleActivityRecognition != PermissionStatus.granted ||
hasGoogleLocationPermission != PermissionStatus.granted) {
return false;
}
...
try {
final isAuthorized =
await health.requestAuthorization(types, permissions: permissions);
return isAuthorized;
} catch (error) {
return false;
}
...
Future<List<HealthDataPoint>> getWorkoutActivitiesByDay(DateTime date) async {
DateTime startOfDay = date.toStartOfDay();
DateTime endOfDay = date.toEndOfDay();
final workouts = await health.getHealthDataFromTypes(startOfDay, endOfDay, [
HealthDataType.WORKOUT,
]);
return workouts;
}
I'm sure that health has acces because after await health.requestAuthorization(types, permissions: permissions);
is returned, then I/FLUTTER_HEALTH(26665): Access Granted!
is printed in the terminal.
When I try await health.isDataTypeAvailable(HealthDataType.WORKOUT)
, it returns true.
I have added the following permissions to my AndroidManifest.xml file:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.health.READ_DISTANCE"/>
<uses-permission android:name="android.permission.health.READ_HEART_RATE"/>
<uses-permission android:name="android.permission.health.READ_EXERCISE"/>
<uses-permission android:name="android.permission.health.READ_STEPS"/>
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
I have configured the OAuth 2.0 credentials in Google Cloud for both Android and iOS. The setup appears to be correct, as steps data fetching works as expected.
Upvotes: 1
Views: 490