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

[health 11.1.0] NullPointerException in getHealthAggregateDataFromTypes (Android Health Connect) #1083

Open
koenmuilwijk opened this issue Nov 20, 2024 · 2 comments
Labels
bugfix a bug fix

Comments

@koenmuilwijk
Copy link

Plugin Name

health

Plugin Version

11.1.0

Device

Android

Operating System

14

Describe the bug

There appears to be a mismatch of argument names between Flutter bridge and Kotlin code:

Compare health_plugin.dart:

  Future<List<HealthDataPoint>> _dataAggregateQuery(
      DateTime startDate,
      DateTime endDate,
      List<HealthDataType> dataTypes,
      int activitySegmentDuration,
      bool includeManualEntry) async {
    final args = <String, dynamic>{
      'dataTypeKeys': dataTypes.map((dataType) => dataType.name).toList(),
      'startTime': startDate.millisecondsSinceEpoch,
      'endTime': endDate.millisecondsSinceEpoch,
      'activitySegmentDuration': activitySegmentDuration,
      'includeManualEntry': includeManualEntry
    };

    final fetchedDataPoints =
        await _channel.invokeMethod('getAggregateData', args);

compare with Kotlin implementation:

        val dataType = call.argument<String>("dataTypeKey")!!
        val interval = call.argument<Long>("interval")!!
        val startTime = Instant.ofEpochMilli(call.argument<Long>("startTime")!!)
        val endTime = Instant.ofEpochMilli(call.argument<Long>("endTime")!!)

And note the difference in parameter names for dataTypeKey vs dataTypeKeys and also for interval vs activitySegmentDuration.

Steps to Reproduce

Calling getHealthAggregateDataFromTypes with AndroidHealthConnect raises java.lang.NullPointerException.

var aggregatedResult = await Health().getHealthAggregateDataFromTypes(
        types: [ HealthDataType.STEPS ],
        startDate: from,
        endDate: till,
        includeManualEntry: false,
        activitySegmentDuration: 3600,
      );

Expected Behavior

Return data aggregated in buckets of specified interval.

Actual Behavior

Throwing java.lang.NullPointerException

Flutter Logs

No response

Screenshots

No response

Flutter Doctor Output

[√] Flutter (Channel stable, 3.24.2, on Microsoft Windows [Version 10.0.22631.4460], locale en-NL)
[√] 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 Community 2022 17.9.4)
    X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop development with C++" workload, and
      include these components:
        MSVC v142 - VS 2019 C++ x64/x86 build tools
         - If there are multiple build tool versions available, install the latest
        C++ CMake tools for Windows
        Windows 10 SDK
[√] Android Studio (version 2024.1)
[√] Android Studio (version 2022.2)
[√] VS Code (version 1.92.2)
[√] Connected device (4 available)
[√] Network resources

Additional Information

No response

@koenmuilwijk koenmuilwijk added the bugfix a bug fix label Nov 20, 2024
@devnta
Copy link

devnta commented Dec 5, 2024

I have same issue.
My code:

final endTime = DateTime.now();
final startTime = endTime.copyWith(hour: 0, minute: 0, second: 0);
final result = await Health().getHealthAggregateDataFromTypes(
        types: types,
        startDate: startTime,
        endDate: endTime,
      );

My types and permissions:

final types = [
    // HealthDataType.DISTANCE_WALKING_RUNNING,
    HealthDataType.ACTIVE_ENERGY_BURNED,
    HealthDataType.STEPS,
  ];

  List<HealthDataAccess> get permissions =>
      types.map((type) => HealthDataAccess.READ_WRITE).toList();

My log:

I/flutter (22552): PlatformException(error, null, null, java.lang.NullPointerException
I/flutter (22552): 	at cachet.plugins.health.HealthPlugin.getAggregateData(HealthPlugin.kt:940)
I/flutter (22552): 	at cachet.plugins.health.HealthPlugin.onMethodCall(HealthPlugin.kt:156)
I/flutter (22552): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
I/flutter (22552): 	at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
I/flutter (22552): 	at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
I/flutter (22552): 	at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
I/flutter (22552): 	at android.os.Handler.handleCallback(Handler.java:958)
I/flutter (22552): 	at android.os.Handler.dispatchMessage(Handler.java:99)
I/flutter (22552): 	at android.os.Looper.loopOnce(Looper.java:230)
I/flutter (22552): 	at android.os.Looper.loop(Looper.java:319)
I/flutter (22552): 	at android.app.ActivityThread.main(ActivityThread.java:8919)
I/flutter (22552): 	at java.lang.reflect.Method.invoke(Native Method)
I/flutter (22552): 	at com.android.internal.os.RuntimeInit$Me
I/flutter (22552): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648:7)
I/flutter (22552): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
I/flutter (22552): <asynchronous suspension>
I/flutter (22552): #2      Health._dataAggregateQuery (package:health/src/health_plugin.dart:1058:9)
I/flutter (22552): <asynchronous suspension>
I/flutter (22552): #3      Health._prepareAggregateQuery (package:health/src/health_plugin.dart:976:12)
I/flutter (22552): <asynchronous suspension>
I/flutter (22552): #4      Health.getHealthAggregateDataFromTypes (package:health/src/health_plugin.dart:902:20)
I/flutter (22552): <asynchronous suspension>

@devnta
Copy link

devnta commented Dec 5, 2024

On iOS, it does not even throw any exception.
Nothing happens on iOS.

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