rmtmckenzie / flutter_qr_mobile_vision

QR reader plugin using mobile vision API for Flutter.
MIT License
217 stars 186 forks source link

NullPointerException in onRequestPermissionsResult when using qr_mobile_vision #236

Closed matheuslns closed 1 month ago

matheuslns commented 1 month ago

Description: I am encountering a crash in my Flutter app immediately after launching the app on specific Android devices when using the qr_mobile_vision package. The app crashes as soon as it opens, and the error occurs when trying to handle permission requests. Below is the full stack trace of the error:

Stack Trace:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=1, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {br.com.claropay.claropay/com.diletta.claropay.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'io.flutter.plugin.common.j$d com.github.rmtmckenzie.qr_mobile_vision.k$b.c' on a null object reference in method 'void com.github.rmtmckenzie.qr_mobile_vision.k.b(java.lang.Throwable)'
    at android.app.ActivityThread.deliverResults(ActivityThread.java:5835)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:5879)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:70)
    ...
Caused by: java.lang.NullPointerException: Attempt to read from field 'io.flutter.plugin.common.j$d com.github.rmtmckenzie.qr_mobile_vision.k$b.c' on a null object reference in method 'void com.github.rmtmckenzie.qr_mobile_vision.k.b(java.lang.Throwable)'
    at com.github.rmtmckenzie.qr_mobile_vision.k.b(Unknown Source:23)
    at com.github.rmtmckenzie.qr_mobile_vision.k.onRequestPermissionsResult(Unknown Source:35)
    ...

Steps to Reproduce:

  1. Use the qr_mobile_vision package in a Flutter app.
  2. Launch the app on an Android device (tested on Xiaomi POCO X4 GT, Android 14).
  3. The app crashes immediately upon opening with the stack trace above.

Expected Behavior: The app should open and handle permission requests without crashing, allowing the camera functionality to work properly.

Environment:

Additional Context: The crash seems to be caused by a NullPointerException when handling the permission result. I have verified that the required camera permissions are declared in the AndroidManifest.xml, and the crash happens as soon as the app launches.

Please let me know if you need further details to assist in resolving this issue.

matheuslns commented 1 month ago

I discovered the problem: the qr_mobile_vision package and flutter_local_notifications use the same code to request permissions, which is why it causes an error when used together.

The solution is to modify the REQUEST_PERMISSION field to a unique code.

Package: qr_mobile_vision File: QrMobileVisionPlugin.java

private static final int REQUEST_PERMISSION = 1;

Package: flutter_local_notifications File: FlutterLocalNotificationsPlugin.java

static final int NOTIFICATION_PERMISSION_REQUEST_CODE = 1;