From fb64d9afc9955aa6dab82d7eb954bcf9ad169cfd Mon Sep 17 00:00:00 2001 From: Bang-ukpc Date: Mon, 12 Jan 2026 03:48:15 +0000 Subject: [PATCH] Fix: Add initialization checks for bluetoothService and adapter in cleanup methods - Fix crash when BackgroundService stops before onAttachedToActivity is called - Add check in onDetachedFromEngine() for bluetoothService and adapter - Add check in onDetachedFromActivityForConfigChanges() for bluetoothService - Add check in onReattachedToActivityForConfigChanges() for bluetoothService - Add check in onDetachedFromActivity() for bluetoothService This prevents UninitializedPropertyAccessException when plugin cleanup happens before lateinit properties are initialized. Fixes issue: kotlin.UninitializedPropertyAccessException: lateinit property bluetoothService has not been initialized --- .../FlutterPosPrinterPlatformPlugin.kt | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/android/src/main/kotlin/com/sersoluciones/flutter_pos_printer_platform/FlutterPosPrinterPlatformPlugin.kt b/android/src/main/kotlin/com/sersoluciones/flutter_pos_printer_platform/FlutterPosPrinterPlatformPlugin.kt index 53340209..7bfb2d76 100644 --- a/android/src/main/kotlin/com/sersoluciones/flutter_pos_printer_platform/FlutterPosPrinterPlatformPlugin.kt +++ b/android/src/main/kotlin/com/sersoluciones/flutter_pos_printer_platform/FlutterPosPrinterPlatformPlugin.kt @@ -177,8 +177,16 @@ class FlutterPosPrinterPlatformPlugin : FlutterPlugin, MethodCallHandler, Plugin messageChannel = null messageUSBChannel = null - bluetoothService.setHandler(null) - adapter.setHandler(null) + // Check if bluetoothService is initialized before cleanup + // This prevents crash when BackgroundService stops before onAttachedToActivity is called + if (::bluetoothService.isInitialized) { + bluetoothService.setHandler(null) + } + + // Check if adapter is initialized before cleanup + if (::adapter.isInitialized) { + adapter.setHandler(null) + } } override fun onAttachedToActivity(binding: ActivityPluginBinding) { @@ -227,7 +235,10 @@ class FlutterPosPrinterPlatformPlugin : FlutterPlugin, MethodCallHandler, Plugin override fun onDetachedFromActivityForConfigChanges() { Log.d(TAG, "onDetachedFromActivityForConfigChanges") currentActivity = null - bluetoothService.setActivity(null) + // Check if bluetoothService is initialized before cleanup + if (::bluetoothService.isInitialized) { + bluetoothService.setActivity(null) + } } override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { @@ -235,13 +246,19 @@ class FlutterPosPrinterPlatformPlugin : FlutterPlugin, MethodCallHandler, Plugin currentActivity = binding.activity binding.addRequestPermissionsResultListener(this) binding.addActivityResultListener(this) - bluetoothService.setActivity(currentActivity) + // Check if bluetoothService is initialized before setting activity + if (::bluetoothService.isInitialized) { + bluetoothService.setActivity(currentActivity) + } } override fun onDetachedFromActivity() { Log.d(TAG, "onDetachedFromActivity") currentActivity = null - bluetoothService.setActivity(null) + // Check if bluetoothService is initialized before cleanup + if (::bluetoothService.isInitialized) { + bluetoothService.setActivity(null) + } } override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {