cph-cachet / flutter-plugins

A collection of Flutter plugins developed by CACHET
551 stars 681 forks source link

[health 10.2.0] Android app crashes when reading health data ? #1030

Closed harryandroiddev closed 2 months ago

harryandroiddev commented 2 months ago

Device / Emulator and OS

Describe the bug

I have used steps , sleep , calories and workout data in my app by health connect . I have asked for all the health data permissions at once before fetching health data.

There are three cases for permissions that user may give:

  1. User granted permission for all.
  2. Partial permission ( e.g steps is not granted , other are granted )
  3. All permissions are denied.

1st and 3rd case are handled , but in case of 2nd the app crashes if i fetch data for reading steps , i have used try and catch but still the app crashes .

To Reproduce

  1. Ask for the permission for health data types .
  2. Allow only few permissions , not all .
  3. Read data for the denied permission .
  4. The app will crash .

Expected behavior

The App should catch the error and not to crash.

Actual behavior

The app is crashing when i read data for ungranted permission

Screenshots

If applicable, add screenshots to help explain your problem.

Flutter doctor

[✓] Flutter (Channel stable, 3.22.2, on macOS 14.4.1 23E224 darwin-arm64, locale en-IN) [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0-rc2) [✓] Xcode - develop for iOS and macOS (Xcode 15.3) [✓] Chrome - develop for the web [✓] Android Studio (version 2024.1) [✓] VS Code (version 1.92.1) [✓] Connected device (5 available)

Additional information

Tried using hasPermission method , and requestauthorization. but still not get the issue fixed . Used try catch while reading data. This behaviour is only on android , on ios the app is not crashing .

Error output

E/AndroidRuntime(23474): FATAL EXCEPTION: main E/AndroidRuntime(23474): Process: com.glucofit.app, PID: 23474 E/AndroidRuntime(23474): java.lang.SecurityException: android.health.connect.HealthConnectException: java.lang.SecurityException: Caller doesn't have android.permission.health.READ_STEPSto read to record typeclass android.health.connect.datatypes.StepsRecord E/AndroidRuntime(23474): at androidx.health.connect.client.impl.platform.ExceptionConverterKt.toKtException(ExceptionConverter.kt:33) E/AndroidRuntime(23474): at androidx.health.connect.client.impl.HealthConnectClientUpsideDownImpl.wrapPlatformException(HealthConnectClientUpsideDownImpl.kt:349) E/AndroidRuntime(23474): at androidx.health.connect.client.impl.HealthConnectClientUpsideDownImpl.access$wrapPlatformException(HealthConnectClientUpsideDownImpl.kt:68) E/AndroidRuntime(23474): at androidx.health.connect.client.impl.HealthConnectClientUpsideDownImpl$wrapPlatformException$1.invokeSuspend(Unknown Source:15) E/AndroidRuntime(23474): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) E/AndroidRuntime(23474): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) E/AndroidRuntime(23474): at android.os.Handler.handleCallback(Handler.java:958) E/AndroidRuntime(23474): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(23474): at android.os.Looper.loopOnce(Looper.java:230) E/AndroidRuntime(23474): at android.os.Looper.loop(Looper.java:319) E/AndroidRuntime(23474): at android.app.ActivityThread.main(ActivityThread.java:8893) E/AndroidRuntime(23474): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(23474): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608) E/AndroidRuntime(23474): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103) E/AndroidRuntime(23474): Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@9ca3c76, Dispatchers.Main]

AkinchanKushwaha commented 2 months ago

can you share the code of your AndroidManifest.xml. You need to add <uses-permission android:name="android.permission.health.READ_STEPS" /> in your manifest file.

harryandroiddev commented 2 months ago

@AkinchanKushwaha Its already there .

harryandroiddev commented 2 months ago
if (Platform.isAndroid) {

      var check = await Health().hasPermissions([HealthDataType.STEPS],
          permissions: [HealthDataAccess.READ]);
      stepsAuth = check;

      if (check == null || !check) {
        return;
      }
    }