Baseflow / flutter-permission-plugins

This repo contains a collection of permission related Flutter plugins which can be used to request permissions to access device resources in a cross-platform way.
https://baseflow.com
MIT License
52 stars 34 forks source link

requestPermissions fails after checkPermissionStatus is called on iOS #7

Closed 1rgs closed 5 years ago

1rgs commented 5 years ago

🐛 Bug Report

If requestPermissions is called, this exception is thrown:

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: PlatformException(ERROR_ALREADY_REQUESTING_PERMISSION, A request for permissions is already running, please wait for it to finish before doing another request., null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564)
#1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:302)
<asynchronous suspension>
#2      LocationPermissions.requestPermissions (package:location_permissions/src/location_permissions.dart:70)
<asynchronous suspension>
#3      LocationRequest._requestPerms (package:social_dating/location_request.dart:15)
<asynchronous suspension>
#4      LocationRequest.build.<anonymous closure> (package:social_dating/location_request.dart:51)
#5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:511)
#6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:566)
#7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:166)
#8      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:240)
#9      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:177)
#10     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:436)
#11     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73)
#12     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101)
#13     _RenderingFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:221)
#14     _RenderingFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:199)
#15     _RenderingFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156)
#16     _RenderingFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102)
#17     _RenderingFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86)
#18     _rootRunUnary (dart:async/zone.dart:1136)
#19     _CustomZone.runUnary (dart:async/zone.dart:1029)
#20     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931)
#21     _invoke1 (dart:ui/hooks.dart:233)
#22     _dispatchPointerDataPacket (dart:ui/hooks.dart:154)

Expected behavior

This should work with no exception.

Reproduction steps

Version: 2.0.0

Platform:

mvanbeusekom commented 5 years ago

@rahulgs12 this exception explains that you have an active request for permissions when a second call to the requestPermissions is coming in. Make sure you await the call to requestPermissions (which you indicate you do) and that you call it only once.

A common mistake is that people call the requestPermissions method from build method in there widget. In some situations this method can be called multiple times by Flutter to organize the UI and in this case also calling the plug-in multiple times.

If you keep on having problems please feel free to send us a code example, even better would be a small app that shows how to reproduce the error and we will be glad to have a look at it.

Viktoru commented 5 years ago

Hello @mvanbeusekom

Thank you for your response. Please, anyone, I need help with this simple geolocation app. It is very simple but I'm having the same problem in ios. It's Working in Android. dependencies: location: ^2.3.5 provider: ^3.0.0+1 geolocator: ^5.1.1+1

info.plist file

NSLocationAlwaysUsageDescription This app needs access to location when in the background. NSLocationAlwaysAndWhenInUseUsageDescription This app needs access to location when open and in the background.

============== main.dart file

import 'dart:async'; import 'package:flutter/material.dart'; import 'package:geolocator/geolocator.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Get User Location', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'Get User Location'), ); } }

class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override _MyHomePageState createState() => _MyHomePageState(); }

class _MyHomePageState extends State {

Position position = null;

void requestLocationPermission(BuildContext context) async { Position currentPosition = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

setState(() {
  position = currentPosition;
});

}

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Requesting Location Permission'), ), body:Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ Padding( padding: const EdgeInsets.all(16.0), child: Center( child: new Text( "Requesting Location Permission...", textAlign: TextAlign.center ), ) ), Padding( padding: const EdgeInsets.symmetric(vertical: 16.0), child: RaisedButton( onPressed: () { requestLocationPermission(context); }, child: Text('Get Location Permission'), color: Colors.green ), ), Center( child: Text(position.toString()), ) ] ), ); } }

======= Debug window:

Syncing files to device iPhone 6s... [VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: PlatformException(ERROR_ALREADY_REQUESTING_PERMISSION, A request for permissions is already running, please wait for it to finish before doing another request., null)

0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)

1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)

#2 LocationPermissions.requestPermissions (package:location_permissions/src/location_permissions.dart:70:45) #3 Geolocator._getLocationPermission (package:geolocator/geolocator.dart:195:12) #4 Geolocator.getCurrentPosition (package:geolocator/geolocator.dart:91:47) #5 _MyHomePageState.requestLocationPermission (package:flutterlocationmap/main.dart:34:51) #6 _MyHomePageState.build. (package:flutterlocationmap/ma<…> Conclusion: It doesn't return the longitude and latitude. Frozen.
Viktoru commented 5 years ago

Sorry, I was missing these: it works

NSLocationWhenInUseUsageDescription This app needs access to location when open. NSLocationAlwaysUsageDescription This app needs access to location when in the background. NSLocationAlwaysAndWhenInUseUsageDescription This app needs access to location when open and in the background.
Theunodb commented 4 years ago

Ps. You need all 4: NSLocationUsageDescription NSLocationAlwaysUsageDescription NSLocationAlwaysAndWhenInUseUsageDescription NSLocationWhenInUseUsageDescription

Only after running the app directly in xcode did the message appear that stated there are missing permissions in the info.plist