Here’s an example of how to use the permission_handler
package in Flutter to check if the user has granted to the app the permissions it needs to function.
Install permission_handler
First, add the permission_handler
package to your pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
permission_handler: ^12.0.2
Then, run flutter pub get
to install the package.
Use it
Now, let’s create a simple Flutter app that requests permission for location:
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _hasPermission = false;
@override
void initState() {
super.initState();
_checkPermission();
}
Future<void> _checkPermission() async {
var isGranted = await Permission.microphone.isGranted;
setState(() {
_hasPermission = isGranted;
});
}
Future<void> _requestPermission() async {
var permission = await Permission.microphone.request();
if (permission == PermissionStatus.granted) {
setState(() {
_hasPermission = true;
});
} else {
setState(() {
_hasPermission = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Permission Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_hasPermission? 'You have granted permission' : 'Please grant permission'),
if (_hasPermission == false)
ElevatedButton(
child: const Text("GIVE Permission"),
onPressed: () {
_requestPermission();
},
),
],
),
),
);
}
}
More About Permissions
In this example, we’re using the Permission.microphone
to request microphone permission. The _checkPermission()
function checks if the permission has been granted and updates the UI accordingly.
Here are some common permissions you might need:
Permission.location
: Requests permission for location.Permission.locationWhenInUse
: Requests permission for location when the app is in use.Permission.locationAlways
: Requests permission for location always.Permission.camera
: Requests permission for camera.Permission.microphone
: Requests permission for microphone.
You can request multiple permissions at once by calling the request()
function with a list of permissions:
var permission = await [
Permission.location,
Permission.camera,
].request();
This way, you can request multiple permissions in a single dialog.
You can also use Permission.request()
with a list of permissions to request multiple permissions at once and handle the result:
var permission = await [
Permission.location,
Permission.camera,
].request();
if (permission == PermissionStatus.granted) {
// All permissions granted
} else if (permission == PermissionStatus.denied) {
// Some or all permissions denied without asking
} else if (permission == PermissionStatus.permanentlyDenied) {
// Some or all permissions permanently denied. You can request again.
}
Permissions Statuses
Statuses about permissions are used in the context of Android permissions, specifically when using the permission_handler
package in Dart.
Here’s a brief explanation of each status:
- isGranted: This indicates that the user has granted the permission to your app.
- isDenied: This means that the user has denied the permission to your app, either permanently or temporarily.
- isPermanentlyDenied: This status is specific to Android 6.0 (Marshmallow) and later versions. It indicates that the user has permanently denied the permission to your app, and it cannot be granted even if the user requests it.
- isProvisional: This status is specific to Android 11 and later versions. It indicates that the user has granted a provisional permission, which is a temporary permission that can be revoked at any time.
- isLimited: This status indicates that the user has granted a limited permission, which restricts some aspects of the permission.
- isRestricted: This status indicates that the user has restricted a permission, which means that some aspects of the permission are not available.