Flutter Location Permission with permission_handler

Flutter Location Permission with permission_handler

Hello All, Here I will explain how to integrate location permission in a flutter. This blog is written for a simple case.

Here you get package information permission_handler

1st Step

Add dependencies in your flutter project. Now the question is in which file do we add dependencies?

Search a pubspec.yaml file in your project

image.png

Open a pubspec.yaml file and search for dependencies: section in pubspec.yaml file.

Now add a dependency. For example, check the below image. Please check the latest version here

image.png

Now run a command in the terminal to download the package and store it locale.

flutter pub get

So Now the dependencies setup is completed. Let’s move to the next step

2nd Step

Now we require to add permission for both platforms(Android/iOS). Let’s start with Android

In android, we require to add permission in the AndroidManifest.xml file. Now the question is how do we find an AndroidManifest.xml File right? For that please check the below image.

image.png

Now add location permission in the AndroidManifest.xml file. Like below

image.png

Note:- Not required to add all this permission. Add base on your requirement.

Let’s add permission in iOS

In iOS, we require to add location permission Info.plist.

image.png

Now open an Info.plist file and add the below code based on the requirement.

image.png

Change a string text to know why your application requires this permission.

In iOS, all the permissions are default disabled. So How we can enable required permission in iOS right?

For that first find a Podfile file in your iOS code.

image.png

In a PodFile first check, the below section is already been added or not.

image.png

Now If the section is not added then add the below section. Also, you can find this section here

post_install do |installer|
 installer.pods_project.targets.each do |target|
 target.build_configurations.each do |config|# Here are some configurations automatically generated by flutter
# You can enable the permissions needed here. For example to enable the camera
 # permission, just remove the `#` character in front so it looks like this:
 #
 # ## dart: PermissionGroup.camera
 # 'PERMISSION_CAMERA=1'
 #
 # Preprocessor definitions can be found in: https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
 config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
 '$(inherited)',
## dart: PermissionGroup.calendar
 # 'PERMISSION_EVENTS=1',
## dart: PermissionGroup.reminders
 # 'PERMISSION_REMINDERS=1',
## dart: PermissionGroup.contacts
 # 'PERMISSION_CONTACTS=1',
## dart: PermissionGroup.camera
 # 'PERMISSION_CAMERA=1',
## dart: PermissionGroup.microphone
 # 'PERMISSION_MICROPHONE=1',
## dart: PermissionGroup.speech
 # 'PERMISSION_SPEECH_RECOGNIZER=1',
## dart: PermissionGroup.photos
 # 'PERMISSION_PHOTOS=1',
## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
 # 'PERMISSION_LOCATION=1',
## dart: PermissionGroup.notification
 # 'PERMISSION_NOTIFICATIONS=1',
## dart: PermissionGroup.mediaLibrary
 # 'PERMISSION_MEDIA_LIBRARY=1',
## dart: PermissionGroup.sensors
 # 'PERMISSION_SENSORS=1',
## dart: PermissionGroup.bluetooth
 # 'PERMISSION_BLUETOOTH=1',
## dart: PermissionGroup.appTrackingTransparency
 # 'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
## dart: PermissionGroup.criticalAlerts
 # 'PERMISSION_CRITICAL_ALERTS=1'
 ]
end
end
end

If a section is already added then just add the below section. below this line.

flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|# Here are some configurations automatically generated by flutter
# You can enable the permissions needed here. For example to enable the camera
# permission, just remove the `#` character in front so it looks like this:
# ## dart: PermissionGroup.camera
# 'PERMISSION_CAMERA=1'
# Preprocessor definitions can be found in: https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## dart: PermissionGroup.calendar
# 'PERMISSION_EVENTS=1',
## dart: PermissionGroup.reminders
# 'PERMISSION_REMINDERS=1',
## dart: PermissionGroup.contacts
# 'PERMISSION_CONTACTS=1',
## dart: PermissionGroup.camera
# 'PERMISSION_CAMERA=1',
## dart: PermissionGroup.microphone
# 'PERMISSION_MICROPHONE=1',
## dart: PermissionGroup.speech
# 'PERMISSION_SPEECH_RECOGNIZER=1',
## dart: PermissionGroup.photos
# 'PERMISSION_PHOTOS=1',
## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
'PERMISSION_LOCATION=1',
## dart: PermissionGroup.notification
# 'PERMISSION_NOTIFICATIONS=1',
## dart: PermissionGroup.mediaLibrary
# 'PERMISSION_MEDIA_LIBRARY=1',
## dart: PermissionGroup.sensors
# 'PERMISSION_SENSORS=1',
## dart: PermissionGroup.bluetooth
# 'PERMISSION_BLUETOOTH=1',
## dart: PermissionGroup.appTrackingTransparency
# 'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
## dart: PermissionGroup.criticalAlerts
# 'PERMISSION_CRITICAL_ALERTS=1'
]
end

Now both Android/iOS setups are completed. Let’s move to the next step

3rd Step

Now first import the package into your dart file

import 'package:permission_handler/permission_handler.dart';

Now First we require to check location service is enabled.

image.png

After location permission is enabled. First, we require to check location permission is already granted or not.

image.png

Now if location permission is not granted. Then we require to write a code to take location permission from a device.

image.png

And based on result status. You can perform the next action.

Now here the question is if the user is permanently denied the permission then How do we handle in case? For this case, we require to open a setting screen to user allow location permission for our application.

image.png

Now all cases are handled.

There are many other dependencies available to enable location permission for the application.

Thank you, and feel free to add comments for questions and suggestions. I will try my best to improve it.