cph-cachet / flutter-plugins

A collection of Flutter plugins developed by CACHET
528 stars 628 forks source link

[HEALTH 8.1.0] NutritionHealthValue throws exception when requesting Nutrition data #871

Open JuanJoseJ opened 6 months ago

JuanJoseJ commented 6 months ago

Remember to specify the plugin name in the title!

Device / Emulator and OS

Please complete the following information for each phone and/or emulator you're experiencing this bug on:

NB: Bugs pertaining to old devices/OS versions will likely not be fixed.

Describe the bug

When using HealthFactory.getHealthDataFromTypes(***) to get Nutrition data from Google Fit the error 'type 'Null' is not a subtype of type 'String' in type cast' is thrown. The error origins in the class NutritionHealthValue, when the function NutritionHealthValue.fromJson(json) is used, all the json's values can be null, with exception to to the calories, and including the mealType, therefore, an exception when "calling json['mealType'] as String" originates.

To Reproduce

use HealthFactory as: ` HealthFactory health = HealthFactory(useHealthConnectIfAvailable: true); DateType start = DateType.now(); DateType end = endtOfDay.subtract(Duration(days: nOfDays)) final List type = [ HealthDataType.MOVE_MINUTES, HealthDataType.SLEEP_ASLEEP, HealthDataType.NUTRITION, ] ; Future<List> fetchHealthData( DateTime start, DateTime end, List types) async { List healthDataList = []; try { healthDataList = await health.getHealthDataFromTypes(start, end, types); } catch (error) { print("Exception in getHealthDataFromTypes: $error"); } return healthDataList; }

List fetchedData fetchedData = await healthDataService.fetchHealthData(endtOfDay.subtract(Duration(days: nOfDays)), endtOfDay, type); `

Expected behavior

A list of HeathDataPoints was expected as a return value.

Actual behavior

Got the following error printed: "Exception in getHealthDataFromTypes: type 'Null' is not a subtype of type 'String' in type cast"

Screenshots

If applicable, add screenshots to help explain your problem.

Flutter doctor

[√] Flutter (Channel stable, 3.13.9, on Microsoft Windows [Versi¢n 10.0.19045.3803], locale es-CO) [√] 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 [√] Visual Studio - develop Windows apps (Visual Studio Build Tools 2019 16.11.10) [√] Android Studio (version 2022.3) [√] IntelliJ IDEA Ultimate Edition (version 2023.1) [√] VS Code (version 1.85.1) [√] Connected device (4 available) [√] Network resources

• No issues found!

Additional information

I'm new to flutter, sorry if it's my bad! This is for my university thesis :(

JuanJoseJ commented 6 months ago

A related problem is that the values fetched are null, a single data point look's like this: I/flutter (12928): [HealthDataPoint - I/flutter (12928): value: protein: null, I/flutter (12928): calories: null, I/flutter (12928): fat: null, I/flutter (12928): name: null,I/flutter (12928): carbs: null, I/flutter (12928): mealType: null, I/flutter (12928): unit: NO_UNIT, I/flutter (12928): dateFrom: 2024-01-06 04:13:00.000, I/flutter (12928): dateTo: 2024-01-06 04:13:00.000, I/flutter (12928): dataType: NUTRITION, I/flutter (12928): platform: PlatformType.ANDROID, I/flutter (12928): deviceId: SKB0.000000.000, I/flutter (12928): sourceId: raw:com.google.nutrition:com.google.android.apps.myApp:user_input, I/flutter (12928): sourceName: com.google.android.apps.fitness, HealthDataPoint -]