Flutter-permission-handler: Android app crashes after permission request popup

Created on 14 Feb 2019  路  27Comments  路  Source: Baseflow/flutter-permission-handler

馃悰 Bug Report

Android app crashes first time request permission when launching, on both deny and grant action . The app restarts, it works fine then.

Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.location]);

D/AndroidRuntime( 8448): Shutting down VM
E/AndroidRuntime( 8448): FATAL EXCEPTION: main
E/AndroidRuntime( 8448): Process: com.berkey.berkey, PID: 8448
E/AndroidRuntime( 8448): java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=25, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.berkey.berkey/com.berkey.berkey.MainActivity}: java.lang.IllegalStateException: Reply already submitted
E/AndroidRuntime( 8448):    at android.app.ActivityThread.deliverResults(ActivityThread.java:4268)
E/AndroidRuntime( 8448):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
E/AndroidRuntime( 8448):    at android.app.ActivityThread.-wrap19(Unknown Source:0)
E/AndroidRuntime( 8448):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
E/AndroidRuntime( 8448):    at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime( 8448):    at android.os.Looper.loop(Looper.java:164)
E/AndroidRuntime( 8448):    at android.app.ActivityThread.main(ActivityThread.java:6494)
E/AndroidRuntime( 8448):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 8448):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
E/AndroidRuntime( 8448):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
E/AndroidRuntime( 8448): Caused by: java.lang.IllegalStateException: Reply already submitted
E/AndroidRuntime( 8448):    at io.flutter.view.FlutterNativeView$1.reply(FlutterNativeView.java:174)
E/AndroidRuntime( 8448):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:203)
E/AndroidRuntime( 8448):    at com.baseflow.permissionhandler.PermissionHandlerPlugin.processResult(PermissionHandlerPlugin.kt:316)
E/AndroidRuntime( 8448):    at com.baseflow.permissionhandler.PermissionHandlerPlugin.handlePermissionsRequest(PermissionHandlerPlugin.kt:308)
E/AndroidRuntime( 8448):    at com.baseflow.permissionhandler.PermissionHandlerPlugin.access$handlePermissionsRequest(PermissionHandlerPlugin.kt:28)
E/AndroidRuntime( 8448):    at com.baseflow.permissionhandler.PermissionHandlerPlugin$Companion$registerWith$1.onRequestPermissionsResult(PermissionHandlerPlugin.kt:46)
E/AndroidRuntime( 8448):    at io.flutter.app.FlutterPluginRegistry.onRequestPermissionsResult(FlutterPluginRegistry.java:191)
E/AndroidRuntime( 8448):    at io.flutter.app.FlutterActivityDelegate.onRequestPermissionsResult(FlutterActivityDelegate.java:125)
E/AndroidRuntime( 8448):    at io.flutter.app.FlutterActivity.onRequestPermissionsResult(FlutterActivity.java:133)
E/AndroidRuntime( 8448):    at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7429)
E/AndroidRuntime( 8448):    at android.app.Activity.dispatchActivityResult(Activity.java:7280)
E/AndroidRuntime( 8448):    at android.app.ActivityThread.deliverResults(ActivityThread.java:4264)
E/AndroidRuntime( 8448):    ... 9 more
Lost connection to device.

Version: 2.2.0

Platform:

  • [ ] :iphone: iOS
  • [x ] :robot: Android

Most helpful comment

The error is possiblly open_file plugin's

how to reproduct

  1. install flutter_bugly, open_file, permissions_handler
  2. use open_file's recommend file_provider in manifest
  3. run app and request storage permission....then boom!

how to fix
comment the line registrar.addRequestPermissionsResultListener(plugin) in OpenFilePlugin.java (abount line 61).

All 27 comments

Same issue for me but iOS App. Running in simulator the app crashes after accept the permissions.

Map requestFuture = await PermissionHandler().requestPermissions([
PermissionGroup.camera,
PermissionGroup.mediaLibrary,
PermissionGroup.location,
PermissionGroup.locationAlways,
PermissionGroup.locationWhenInUse
]);

Version: 2.2.0

[x ]馃摫 iOS
馃 Android

@comeonbuddy, @fpiruzi, I cannot really reproduce this issue on my system (also example App works fine on both platforms).

Would one of you be able to supply a small example which reproduces this error?

In my system when I start it in debug session, everything works fine, but release version behaves in the way described: app crashes after permission is granted. My code to reproduce:
void _scanBluetoothDevices() async {
PermissionStatus permission = await PermissionHandler()
.checkPermissionStatus(PermissionGroup.location);
if (permission == PermissionStatus.granted) {
viewModel.subscribeToStateChanges();
viewModel.startGettingValuesFast();
} else {
PermissionHandler()
.requestPermissions([PermissionGroup.location]);
_scanBluetoothDevices();
}
}
It is called from initState()

even when I live this function without a recursive call and any additional code it still closes the app
void _scanBluetoothDevices() async {
PermissionStatus permission = await PermissionHandler()
.checkPermissionStatus(PermissionGroup.location);
if (permission == PermissionStatus.granted) {
} else {
PermissionHandler()
.requestPermissions([PermissionGroup.location]);
}
}

same issue

use permission_handler: ^3.0.0 fixed it

I tried it and still have the same issue

Seems like I post a bit incorrect code. I used await PermissionHandler()
.requestPermissions([PermissionGroup.location]); So the problem is inside the waiting for the result from this function

Finally, I found what was the problem in my code. I wanted to start Bluetooth search and do it when app is in the foreground only, that's why I observe WidgetsBinding.instance. In debug mode, this observer isn't called when the app becomes active for the first time (so I called a method to request permissions from onCreate and from the observer. But in release mode, the observer is called even when activity became visible for the first time! That's why I requested permissions twice. Now I fixed it with setting a flag indicating that a request is still in progress. It solved the crash.

[+3185 ms] D/AndroidRuntime( 8380): Shutting down VM
[ +1 ms] E/AndroidRuntime( 8380): FATAL EXCEPTION: main
[ ] E/AndroidRuntime( 8380): Process: com.example.mangaapp, PID: 8380
[ ] E/AndroidRuntime( 8380): java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=25, result=-1, data=Intent {
act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.example.mangaapp/com.example.mangaapp.MainActivity}: java.lang.NullPointerException
[ ] E/AndroidRuntime( 8380): at android.app.ActivityThread.deliverResults(ActivityThread.java:4324)
[ ] E/AndroidRuntime( 8380): at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367)
[ ] E/AndroidRuntime( 8380): at android.app.ActivityThread.-wrap19(Unknown Source:0)
[ ] E/AndroidRuntime( 8380): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649)
[ ] E/AndroidRuntime( 8380): at android.os.Handler.dispatchMessage(Handler.java:105)
[ ] E/AndroidRuntime( 8380): at android.os.Looper.loop(Looper.java:164)
[ ] E/AndroidRuntime( 8380): at android.app.ActivityThread.main(ActivityThread.java:6541)
[ ] E/AndroidRuntime( 8380): at java.lang.reflect.Method.invoke(Native Method)
[ ] E/AndroidRuntime( 8380): at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
[ ] E/AndroidRuntime( 8380): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
[ ] E/AndroidRuntime( 8380): Caused by: java.lang.NullPointerException
[ ] E/AndroidRuntime( 8380): at java.io.File.(File.java:282)
[ ] E/AndroidRuntime( 8380): at com.crazecoder.openfile.OpenFilePlugin.startActivity(OpenFilePlugin.java:118)
[ ] E/AndroidRuntime( 8380): at com.crazecoder.openfile.OpenFilePlugin.onRequestPermissionsResult(OpenFilePlugin.java:325)
[ ] E/AndroidRuntime( 8380): at io.flutter.app.FlutterPluginRegistry.onRequestPermissionsResult(FlutterPluginRegistry.java:199)
[ ] E/AndroidRuntime( 8380): at io.flutter.app.FlutterActivityDelegate.onRequestPermissionsResult(FlutterActivityDelegate.java:129)
[ ] E/AndroidRuntime( 8380): at io.flutter.app.FlutterActivity.onRequestPermissionsResult(FlutterActivity.java:137)
[ ] E/AndroidRuntime( 8380): at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7388)
[ ] E/AndroidRuntime( 8380): at android.app.Activity.dispatchActivityResult(Activity.java:7239)
[ ] E/AndroidRuntime( 8380): at android.app.ActivityThread.deliverResults(ActivityThread.java:4320)
[ ] E/AndroidRuntime( 8380): ... 9 more

same issue []: ejava.lang.NullPointerException

Same here it always stops after giving permission with above crash logs. Please fix this fast otherwise how can i use this library thanks.

The error is possiblly open_file plugin's

how to reproduct

  1. install flutter_bugly, open_file, permissions_handler
  2. use open_file's recommend file_provider in manifest
  3. run app and request storage permission....then boom!

how to fix
comment the line registrar.addRequestPermissionsResultListener(plugin) in OpenFilePlugin.java (abount line 61).

Same issue for me in Android: Can request only one set of permissions at a time. PlatformException(ERROR_ALREADY_REQUESTING_PERMISSIONS, A request for permissions is already running.

I'm using Geolocator 3.0.1 on my project.

In the AndroidManifest I have 3 permissions: INTERNET, ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION.

@cronos88, this looks to be a completely different issue. But you are right you need to await the the call to the permission library before requesting new permissions.

You can however request multiple permissions at once (using the same call):

Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.contacts, PermissionGroup.location, ...]);

same problem,how to fix it?

@xuwankang, @nayan2823, @alanppxmw, @ckken, as mentioned earlier we are having difficulty reproducing the error. So simply stating that you experience the same problem or pasting the the stack trace doesn't really help us, help you.

Based on the stack trace it looks like the plugin is trying to access an activity which isn't there anymore. Are you running this code while the App is in the background?

Can you maybe help us out by providing some code or a small sample application to reproduce the error?

PermissionStatus permission = await PermissionHandler() .checkPermissionStatus(PermissionGroup.locationWhenInUse); print('permission${permission}'); if (permission == PermissionStatus.denied) { print("location fail"); Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler() .requestPermissions([PermissionGroup.locationWhenInUse]); if (permissions[PermissionGroup.locationWhenInUse] == PermissionStatus.denied) { print("location fail"); return; } else { print("location success"); } }

dependencies: flutter: sdk: flutter cupertino_icons: ^0.1.2 flutter_localizations: sdk: flutter shared_preferences: ^0.4.3 font_awesome_flutter: ^8.2.0 open_file: ^2.0.1 path_provider: ^0.4.1 http: ^0.12.0 dio: ^1.0.6 cached_network_image: ^0.5.0+1 photo_view: ^0.0.10 flutter_html: ^0.8.2 transparent_image: ^0.1.0 image_picker: ^0.5.0+3 fluttertoast: ^3.0.3 image: ^2.0.4 flutter_swiper: ^1.1.4 flutter_refresh: ^0.0.2 flutter_image_compress: ^0.2.3 flutter_local_notifications: ^0.5.2 flutter_datetime_picker: ^1.0.6 package_info: ^0.4.0+1 amap_location: ^0.1.2 cupertino_range_slider: ^1.0.1 flutter_range_slider: ^1.0.1 permission_handler: ^3.0.0 flutter_screenutil: git: url: git://github.com/openflutter/flutter_screenutil multi_image_picker: ^3.0.21

when app start run ,the menu show up, when i tap allowed ,the app crashed

Fresh install our app on android mobile this issue will be generate, after crashed again open our app this issue is not showing.

_Please fix this isue.it's urgent for my production app_
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=25, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.cbtech.cb_curemantra/com.cbtech.cb_curemantra.MainActivity}: java.lang.IllegalStateException: Reply already submitted
E/AndroidRuntime(24178): at android.app.ActivityThread.deliverResults(ActivityThread.java:4393)
E/AndroidRuntime(24178): at android.app.ActivityThread.handleSendResult(ActivityThread.java:4435)
E/AndroidRuntime(24178): at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
E/AndroidRuntime(24178): at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
E/AndroidRuntime(24178): at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
E/AndroidRuntime(24178): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1838)
E/AndroidRuntime(24178): at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(24178): at android.os.Looper.loop(Looper.java:193)
E/AndroidRuntime(24178): at android.app.ActivityThread.main(ActivityThread.java:6715)
E/AndroidRuntime(24178): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(24178): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/AndroidRuntime(24178): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
E/AndroidRuntime(24178): Caused by: java.lang.IllegalStateException: Reply already submitted
E/AndroidRuntime(24178): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:124)
E/AndroidRuntime(24178): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:204)
E/AndroidRuntime(24178): at com.baseflow.permissionhandler.PermissionHandlerPlugin.processResult(PermissionHandlerPlugin.java:406)
E/AndroidRuntime(24178): at com.baseflow.permissionhandler.PermissionHandlerPlugin.handlePermissionsRequest(PermissionHandlerPlugin.java:397)
E/AndroidRuntime(24178): at com.baseflow.permissionhandler.PermissionHandlerPlugin.access$000(PermissionHandlerPlugin.java:34)
E/AndroidRuntime(24178): at com.baseflow.permissionhandler.PermissionHandlerPlugin$1.onRequestPermissionsResult(PermissionHandlerPlugin.java:126)
E/AndroidRuntime(24178): at io.flutter.app.FlutterPluginRegistry.onRequestPermissionsResult(FlutterPluginRegistry.java:203)
E/AndroidRuntime(24178): at io.flutter.app.FlutterActivityDelegate.onRequestPermissionsResult(FlutterActivityDelegate.java:129)
E/AndroidRuntime(24178): at io.flutter.app.FlutterActivity.onRequestPermissionsResult(FlutterActivity.java:137)
E/AndroidRuntime(24178): at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7608)
E/AndroidRuntime(24178): at android.app.Activity.dispatchActivityResult(Activity.java:7458)
E/AndroidRuntime(24178): at android.app.ActivityThread.deliverResults(ActivityThread.java:4386)
E/AndroidRuntime(24178): ... 11 more
I/Process (24178): Sending signal. PID: 24178 SIG: 9
Application finished.

Please check this thread: https://stackoverflow.com/questions/55312450/permission-handler-issue-in-flutter

I have created a complete example of what is the issue.

hey @mvanbeusekom I think I found how to reproduce this issue. It happens if Request Permission is automatically called while building the widget, it seems the widget tree sometimes gets rebuild a few times and the permission got requested a few times.

Map permissions = await PermissionHandler().requestPermissions([PermissionGroup.location]);

I can't request permission in the constructor because of the async nature of the perm request.
In your example it was done via an onTap event which is clean but how do you auto request when loading?

This issue should be fixed in version 3.0.0

Calling requestPermissions more than once without waiting for the future to resolve caused the issue for me. Check if your widget is not built more than once, or if you aren't accidentally calling the function multiple times. FYI: Calling requestPermissions multiple times will cause the app to crash, even with permission_handler: ^3.0.1.

i have same issue, but this crash only occur on some devices, based on my firebase crashlytics...

I have it too, should be easy to fix with :

Future _currentRequestPermissions;

Future<Map<PermissionGroup, PermissionStatus>> requestPermissions(
      List<PermissionGroup> permissions) async {
    if(_currentRequestPermissions != null) await _currentRequestPermissions;
    final List<int> data = Codec.encodePermissionGroups(permissions);
    _currentRequestPermissions = _methodChannel.invokeMethod('requestPermissions', data);
    final Map<dynamic, dynamic> status =
        await _currentRequestPermissions;
   _currentRequestPermissions = null;

    return Codec.decodePermissionRequestResult(Map<int, int>.from(status));
  }

_The error is possiblly open_file plugin's_

how to reproduct

  1. install flutter_bugly, open_file, permissions_handler
  2. use open_file's recommend file_provider in manifest
  3. run app and request storage permission....then boom!

how to fix
comment the line registrar.addRequestPermissionsResultListener(plugin) in OpenFilePlugin.java (abount line 61).

it really helped me.

@cronos88, this looks to be a completely different issue. But you are right you need to await the the call to the permission library before requesting new permissions.

You can however request multiple permissions at once (using the same call):

Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.contacts, PermissionGroup.location, ...]);

Still facing the same problem with version 4.4.0
The above solution worked for me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marcelderkskarlmax picture marcelderkskarlmax  路  5Comments

Rossdex picture Rossdex  路  5Comments

petrnymsa picture petrnymsa  路  3Comments

VOIDCRUSHER picture VOIDCRUSHER  路  3Comments

jaleelmeemo picture jaleelmeemo  路  5Comments