Skip to content

[health] Distance data is always fetched for workouts regardless of need #1173

@Strime

Description

@Strime

Plugin Name

health

Plugin Version

12.1.0

Device

Pixel 6

Operating System

Android 15

Describe the bug

The health plugin documentation states:

"Additionally, for workouts, if the distance of a workout is requested then the location permissions below are needed."

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

However, I found that the current implementation in HealthPlugin.kt always fetches distance data when retrieving workout information, regardless of whether distance was specifically requested or needed.

In the getData method around line 729, when dataType == WORKOUT, the code always makes a request for distance data using DistanceRecord::class. This forces users to include location permissions even when they don't need distance data for workouts.

Steps to Reproduce

Steps to Reproduce:

  1. Create a Flutter application using the health plugin
  2. Do not include the location permissions in the AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  1. Ask for workout
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
      List<HealthDataPoint> workoutPoints =
          await _health.getHealthDataFromTypes(
        types: [HealthDataType.WORKOUT],
        startTime: startDate,
        endTime: endTime,
      );

Expected Behavior

Distance data should only be fetched when explicitly requested. This would:

  1. Align with the documentation's statement
  2. Allow applications to avoid requiring location permissions when not needed
  3. Potentially improve performance by avoiding unnecessary data requests

Actual Behavior

The app will fail to retrieve data with permission-related errors, even though you don't need the distance component of the workout data.

Flutter Logs

I/FLUTTER_HEALTH::ERROR(26944): Unable to return WORKOUT due to the following exception:
E/FLUTTER_HEALTH::ERROR(26944): java.lang.SecurityException: android.health.connect.HealthConnectException: java.lang.SecurityException: Caller doesn't have android.permission.health.READ_DISTANCE to read to record typeclass android.health.connect.datatypes.DistanceRecord
E/FLUTTER_HEALTH::ERROR(26944): 	at androidx.health.connect.client.impl.platform.ExceptionConverterKt.toKtException(ExceptionConverter.kt:33)
E/FLUTTER_HEALTH::ERROR(26944): 	at androidx.health.connect.client.impl.HealthConnectClientUpsideDownImpl.wrapPlatformException(HealthConnectClientUpsideDownImpl.kt:391)
E/FLUTTER_HEALTH::ERROR(26944): 	at androidx.health.connect.client.impl.HealthConnectClientUpsideDownImpl.access$wrapPlatformException(HealthConnectClientUpsideDownImpl.kt:78)
E/FLUTTER_HEALTH::ERROR(26944): 	at androidx.health.connect.client.impl.HealthConnectClientUpsideDownImpl$wrapPlatformException$1.invokeSuspend(Unknown Source:15)
E/FLUTTER_HEALTH::ERROR(26944): 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/FLUTTER_HEALTH::ERROR(26944): 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
E/FLUTTER_HEALTH::ERROR(26944): 	at android.os.Handler.handleCallback(Handler.java:991)
E/FLUTTER_HEALTH::ERROR(26944): 	at android.os.Handler.dispatchMessage(Handler.java:102)
E/FLUTTER_HEALTH::ERROR(26944): 	at android.os.Looper.loopOnce(Looper.java:232)
E/FLUTTER_HEALTH::ERROR(26944): 	at android.os.Looper.loop(Looper.java:317)
E/FLUTTER_HEALTH::ERROR(26944): 	at android.app.ActivityThread.main(ActivityThread.java:8973)
E/FLUTTER_HEALTH::ERROR(26944): 	at java.lang.reflect.Method.invoke(Native Method)
E/FLUTTER_HEALTH::ERROR(26944): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:591)
E/FLUTTER_HEALTH::ERROR(26944): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:927)

Screenshots

No response

Flutter Doctor Output

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.29.2, on macOS 15.3.2 24D81 darwin-arm64, locale en-FR)
[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/to/macos-android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 16.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.3)
[✓] Android Studio (version 2024.1)
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.98.2)
⣽^[✓] Connected device (3 available)
    ! Error: Browsing on the local area network for iPhone de Gaet. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
[✓] Network resources

! Doctor found issues in 1 category.

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions