Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException when reading from Health Connect, health ^10.2.0 #950

Open
Leonardbd opened this issue Apr 13, 2024 · 1 comment
Open
Labels
bugfix a bug fix

Comments

@Leonardbd
Copy link

Leonardbd commented Apr 13, 2024

Device / Emulator and OS

  • Device: Pixel 4
  • OS: Android 13

Describe the bug

The app crashes when attempting to fetch health data from Health Connect for a start date where there's no data for that date or for any other date prior to the startdate.

To Reproduce

Use the plugin to fetch health data for a date range that includes a start date without any data available in Health Connect prior to this date.
The app crashes when attempting to fetch data for the specified start date.

Health().configure(useHealthConnectIfAvailable: true);
var now = DateTime.now();

var types = [
      HealthDataType.NUTRITION,
    ];
    bool requested = await Health().requestAuthorization(types);

List<HealthDataPoint> healthData = await Health().getHealthDataFromTypes(
        startTime: now.subtract(Duration(days: 4)), endTime: now, types: types);

So today is April 14th, StartTime is then April 10th. The first recorded data available in Health Connect is April 11th, causing it to crash.

Expected behavior

The plugin should handle the scenario gracefully and not crash when attempting to fetch health data for a start date without any available data. Instead of crashing, it should continue to retrieve data for the remaining duration within the specified date range (between the start date and end date)

Actual behavior

The app crashes when attempting to fetch health data for a start date without any available data.

E/AndroidRuntime(17369): FATAL EXCEPTION: main
E/AndroidRuntime(17369): Process: com.applitic.healthtrackerapp, PID: 17369
E/AndroidRuntime(17369): java.lang.NullPointerException
E/AndroidRuntime(17369): 	at cachet.plugins.health.HealthPlugin.convertRecord(HealthPlugin.kt:3297)
E/AndroidRuntime(17369): 	at cachet.plugins.health.HealthPlugin$getHCData$1.invokeSuspend(HealthPlugin.kt:2828)
E/AndroidRuntime(17369): 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/AndroidRuntime(17369): 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
E/AndroidRuntime(17369): 	at android.os.Handler.handleCallback(Handler.java:942)
E/AndroidRuntime(17369): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(17369): 	at android.os.Looper.loopOnce(Looper.java:201)
E/AndroidRuntime(17369): 	at android.os.Looper.loop(Looper.java:288)
E/AndroidRuntime(17369): 	at android.app.ActivityThread.main(ActivityThread.java:7872)
E/AndroidRuntime(17369): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(17369): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/AndroidRuntime(17369): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
E/AndroidRuntime(17369): 	Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@e0bf177, Dispatchers.Main]
I/Process (17369): Sending signal. PID: 17369 SIG: 9
Lost connection to device.

Exited.

Flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.16.5, on Microsoft Windows [Version 10.0.22631.3447], locale en-GB)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[X] Visual Studio - develop Windows apps
X Visual Studio not installed; this is necessary to develop Windows apps.
Download at https://visualstudio.microsoft.com/downloads/.
Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2023.1)
[√] VS Code (version 1.88.1)
[√] Connected device (4 available)
[√] Network resources

! Doctor found issues in 1 category.

@Leonardbd Leonardbd added the bugfix a bug fix label Apr 13, 2024
@Leonardbd Leonardbd changed the title health ^10.2.0 Crashing when reading from Health Connect health ^10.2.0 Apr 14, 2024
@Leonardbd Leonardbd changed the title Crashing when reading from Health Connect health ^10.2.0 Crashing when reading from Health Connect, health ^10.2.0 Apr 14, 2024
@nwhite872
Copy link

Hello, I ran into the same problem and I had an afternoon so I read through the file. The plugin passess the NullPointerException which leads to an issue, so I modified the plugin file on line 3284 of '\AppData\Local\Pub\Cache\hosted\pub.dev\health-10.2.0\android\src\main\kotlin\cachet\plugins\health\HealthPlugin.kt from:

is NutritionRecord ->
    return listOf(
        mapOf<String, Any>(
            "calories" to record.energy!!.inKilocalories,
            "protein" to record.protein!!.inGrams,
            "carbs" to record.totalCarbohydrate!!.inGrams,
            "fat" to record.totalFat!!.inGrams,
            "name" to record.name!!,
            "mealType" to (MapTypeToMealTypeHC[record.mealType] ?: MEAL_TYPE_UNKNOWN),
            "date_from" to record.startTime.toEpochMilli(),
            "date_to" to record.endTime.toEpochMilli(),
            "source_id" to "",
            "source_name" to metadata.dataOrigin.packageName,
        )
    )

Into

is NutritionRecord -> {
  val calories = record.energy?.inKilocalories ?: 0.0
  val protein = record.protein?.inGrams ?: 0.0
  val carbs = record.totalCarbohydrate?.inGrams ?: 0.0
  val fat = record.totalFat?.inGrams ?: 0.0
  val name = record.name ?: ""
  val mealType = MapTypeToMealTypeHC[record.mealType] ?: MEAL_TYPE_UNKNOWN
  val dateFrom = record.startTime?.toEpochMilli() ?: 0L
  val dateTo = record.endTime?.toEpochMilli() ?: 0L
  val sourceId = ""
  val sourceName = metadata.dataOrigin?.packageName ?: ""

  return listOf(
  mapOf(
  "calories" to calories,
  "protein" to protein,
  "carbs" to carbs,
  "fat" to fat,
  "name" to name,
  "mealType" to mealType,
  "date_from" to dateFrom,
  "date_to" to dateTo,
  "source_id" to sourceId,
  "source_name" to sourceName,
  )
   )
}

Admittedly it is not elegant, but it sets the default values to 0.0 for missing values in carbohydrates/protein/fats which was what was causing my problem. The formating in the HealthPlugin.kt file drove me insane.

I hope this helps. If someone more skilled than I would be kind enough to pull request that would be appreciated.

@Leonardbd Leonardbd changed the title Crashing when reading from Health Connect, health ^10.2.0 NullPointerException when reading from Health Connect, health ^10.2.0 Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix a bug fix
Projects
None yet
Development

No branches or pull requests

2 participants