Skip to content

Health Connect Crashes in Android - Tiramisu, for isAPISupported() #28

@Srinivas142000

Description

@Srinivas142000

Hi, I wanted to use this plugin to connect with the health connect to read the data and process it but for some reason when i am using the api to collect permissions before login (which is the standard practice) it crashes the app and doesnt let me in. I have identified this as one of the reasons and any help would be appreciated.

Here is my code

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  // Ensure Health Connect is installed before proceeding
  bool isInstalled = await ensureHealthConnectInstalled();

  if (isInstalled) {
    // Request permissions before launching the app
    bool permissionsGranted = await _requestPermissions();
    UserSession session = UserSession();
    session.savePermissions(permissionsGranted);
  }

  runApp(MyApp(isHealthConnectInstalled: isInstalled));
}

/// Ensures Health Connect is installed before proceeding
Future<bool> ensureHealthConnectInstalled() async {
  try {
    bool isAvailable = await HealthConnectFactory.isAvailable();
    if (!isAvailable) {
      print("Health Connect is not installed. Prompting user to install...");
      await HealthConnectFactory.installHealthConnect();
      return false; // App should not continue until installed
    }
    print("Health Connect is installed!");
    return true;
  } catch (err) {
    print("Error checking Health Connect availability: $err");
    return false;
  }
}

/// Requests necessary permissions
Future<bool> _requestPermissions() async {
  Map<Permission, PermissionStatus> statuses = await [
    Permission.activityRecognition,
    Permission.sensors,
    Permission.location,
  ].request();

  final types = [
    HealthDataType.STEPS,
    HealthDataType.DISTANCE_WALKING_RUNNING,
    HealthDataType.TOTAL_CALORIES_BURNED
  ];

  try {
    bool isAvailable = await HealthConnectFactory.isAvailable();
    if (isAvailable) {
      final healthConnectTypes = types.map((type) {
        switch (type) {
          case HealthDataType.STEPS:
            return HealthConnectDataType.Steps;
          case HealthDataType.DISTANCE_WALKING_RUNNING:
            return HealthConnectDataType.Distance;
          case HealthDataType.TOTAL_CALORIES_BURNED:
            return HealthConnectDataType.TotalCaloriesBurned;
          default:
            throw Exception("Unsupported HealthDataType: $type");
        }
      }).toList();

      bool granted =
          await HealthConnectFactory.requestPermissions(healthConnectTypes);
      if (!granted) {
        print("Health Connect permissions not granted!");
        return false;
      }
      print("Health Connect permissions granted!");
    }
  } catch (err) {
    print("Error requesting Health Connect permissions: $err");
    return false;
  }

  return statuses.values.every((status) => status.isGranted);
}

Here is the error i received in this context

E/AndroidRuntime(24501): FATAL EXCEPTION: main E/AndroidRuntime(24501): Process, PID: 24501 E/AndroidRuntime(24501): java.lang.NoSuchMethodError: No static method sdkStatus$default(Landroidx/health/connect/client/HealthConnectClient$Companion;Landroid/content/Context;Ljava/lang/String;ILjava/lang/Object;)I in class Landroidx/health/connect/client/HealthConnectClient$Companion; or its super classes (declaration of 'androidx.health.connect.client.HealthConnectClient$Companion' appears in /data/app/~~mFqM45HGRnTS9iDPVPFQmw==/-AbkLI64FRa26bFdm4hR_tw==/base.apk) E/AndroidRuntime(24501): at dev.duynp.flutter_health_connect.FlutterHealthConnectPlugin.onMethodCall(FlutterHealthConnectPlugin.kt:86) E/AndroidRuntime(24501): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/AndroidRuntime(24501): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292) E/AndroidRuntime(24501): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319) E/AndroidRuntime(24501): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0) E/AndroidRuntime(24501): at android.os.Handler.handleCallback(Handler.java:942) E/AndroidRuntime(24501): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(24501): at android.os.Looper.loopOnce(Looper.java:226) E/AndroidRuntime(24501): at android.os.Looper.loop(Looper.java:313) E/AndroidRuntime(24501): at android.app.ActivityThread.main(ActivityThread.java:8762) E/AndroidRuntime(24501): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(24501): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604) E/AndroidRuntime(24501): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) I/Process (24501): Sending signal. PID: 24501 SIG: 9

Here is my attached Stack overflow question for more context:

[https://stackoverflow.com/questions/79481866/error-with-flutter-health-connect-connecting-to-health-connect-in-android-sdk](StackOverflow Question)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions