I am building a flutter based health app using flutter_health_connect to get details from android but before i use my login (which is the entry point for most applications) i wanted to collect all the permissions and then proceed to login page, So far i am able to collect permissions for location and body sensors but the one i want most the health connect refuses to connect with my app despite using their plugins and API's perfectly.
here is my sample 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);
}
Btw i have installed the Health Connect manually on the device, Not only does this not install or redirect me to the install page if the app is missing, it doesn't care if it is present or not in the first place.it just terminates user from entering the app and crashes it, so far this is the error i have received:
**
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.pluginmon.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
**