Baseflow / flutter-permission-handler

Permission plugin for Flutter. This plugin provides a cross-platform (iOS, Android) API to request and check permissions.
https://baseflow.com
MIT License
2.05k stars 858 forks source link

Android app crashes after permission request popup #76

Closed comeonbuddy closed 5 years ago

comeonbuddy commented 5 years ago

🐛 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:

fpiruzi commented 5 years ago

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

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

Version: 2.2.0

[x ]📱 iOS 🤖 Android

mvanbeusekom commented 5 years ago

@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?

Yahhi commented 5 years ago

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()

Yahhi commented 5 years ago

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]); } }

ckken commented 5 years ago

same issue

ckken commented 5 years ago

use permission_handler: ^3.0.0 fixed it

Yahhi commented 5 years ago

I tried it and still have the same issue

Yahhi commented 5 years ago

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

Yahhi commented 5 years ago

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.

ckken commented 5 years ago

[+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

alanppxmw commented 5 years ago

same issue []: ejava.lang.NullPointerException

nayan2823 commented 5 years ago

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

ColorFuzzy commented 5 years ago

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).

cronos88 commented 5 years ago

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.

mvanbeusekom commented 5 years ago

@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, ...]);
k3v1n-uncle commented 5 years ago

same problem,how to fix it?

mvanbeusekom commented 5 years ago

@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?

k3v1n-uncle commented 5 years ago

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

prasant10050 commented 5 years ago

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.

abumoallim commented 5 years ago

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.

comeonbuddy commented 5 years ago

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<PermissionGroup, PermissionStatus> 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?

mvanbeusekom commented 5 years ago

This issue should be fixed in version 3.0.0

vincevargadev commented 5 years ago

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.

mestartlearncode commented 5 years ago

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

aloisdeniel commented 5 years ago

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));
  }
mateusjs commented 5 years ago

_The error is possiblly openfile 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.

bmabir17 commented 4 years ago

@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

Technorocker commented 2 years ago

I am having this problem right now using version 10.0.0. It was working fine but havent gone through my permissions screen in awhile. I have a dedicated screen right after the splashscreen for fresh installs that checks all permissions needed. So once I'm signed in working on the app i dont see it again till its been uninstalled. now a few days ago I upgraded my flutter from 2.10 to 3.3 so I'm not sure if that might have affected how I have it coded but thats the only thing on the baseline side of things thats really changes since last time i went through the permissions screen.

after a few attempts I noticed that it seems to close or crash the app when the popup starts to close. For instance. I groups the camera and mic permissions together for one button. so the popup opens requesting the camera, I click while using app then it stays open and shows me the mic request which I also click while using app and soon as I see the popup start lowering to close the app shuts down. now the storage request is only the single request and same thing soon as I click while app is open and the popup starts to lower and closes the app crashes.

The request does go through cause when i reopen the app and get to the permissions screen again I can click on the camera request button and it turns green to show its already accepted. hmmm maybe its not a permissions_handler issue and its an android/flutter issues. Any insight would be most helpful.

permissions screen permissions cam req permissions cam accepted