How to Request Single or Multiple Permissions at Once in Flutter App

Do you want to access the location or camera, or anything like that from the device? You need permission from the user first to perform such a task. Some permission works by only declaring at AndroidManifest.xml file, but it better to ask once. See the example below to know more. 

Add Permissions which are going to be requested by adding permission tag in android/app/src/main/AndroidManifest.xml before <application> like below:

<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA" />

Now in Flutter part, add permission_handler Flutter package to dependency by adding the following line in pubspec.yaml file. 

dependencies:
  flutter:
    sdk: flutter
  permission_handler: ^6.1.1

To Check Permission Status:

var status = await Permission.camera.status;
if (status.isDenied) {
  // We didn't ask for permission yet or the permission has been denied before but not permanently.
  print("Permission is denined.");
}else if(status.isGranted){
  //permission is already granted.
  print("Permission is already granted.");
}else if(status.isPermanentlyDenied){
  //permission is permanently denied.
  print("Permission is permanently denied");
}else if(status.isRestricted){
  //permission is OS restricted.
  print("Permission is OS restricted.");
}

Here, we check Camera permission status without a popup. You would know either is granted, denied or permanently denied.

To Request Single Permission:

if (await Permission.location.request().isGranted) {
      // Either the permission was already granted before or the user just granted it.
      print("Location Permission is granted");
}else{
    print("Location Permission is denied.");
}

Here, we have requested location permission.

To Request Multiple Permissions:

// You can request multiple permissions at once.
Map<Permission, PermissionStatus> statuses = await [
  Permission.location, 
  Permission.camera,
  //add more permission to request here.
].request();

if(statuses[Permission.location].isDenied){ //check each permission status after.
    print("Location permission is denied.");
}

if(statuses[Permission.camera].isDenied){ //check each permission status after.
    print("Camera permission is denied.");
}

Here, we request Location and Camera permission at once.

Full Dart Code:

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(
        title: "Test App",
        home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget{
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold( 
      appBar: AppBar( 
         title: Text("Request Permission"),
         backgroundColor: Colors.redAccent,
      ),
      body: Container( 
        alignment: Alignment.center,
        padding: EdgeInsets.all(20),
         child: Column(
           children: [
               Container( 
                 child: ElevatedButton( 
                   child: Text("Request Single Permission"),
                   onPressed: () async {
                     if (await Permission.location.request().isGranted) {
                           // Either the permission was already granted before or the user just granted it.
                           print("Location Permission is granted");
                      }else{
                          print("Location Permission is denied.");
                      }
                   },
                 ),
               ),

               Container( 
                 child: ElevatedButton( 
                   child: Text("Request Multiple Permission"),
                   onPressed: () async {
                      // You can request multiple permissions at once.
                      Map<Permission, PermissionStatus> statuses = await [
                        Permission.location, 
                        Permission.camera,
                        //add more permission to request here.
                      ].request();
                     
                      if(statuses[Permission.location].isDenied){ //check each permission status after.
                          print("Location permission is denied.");
                      }

                      if(statuses[Permission.camera].isDenied){ //check each permission status after.
                          print("Camera permission is denied.");
                      }
                   },
                 ),
               ),

               Container( 
                 child: ElevatedButton( 
                   child: Text("Check Camera Permission"),
                   onPressed: () async {
                    //check permission without request popup
                      var status = await Permission.camera.status;
                      if (status.isDenied) {
                        // We didn't ask for permission yet or the permission has been denied before but not permanently.
                        print("Permission is denined.");
                      }else if(status.isGranted){
                        //permission is already granted.
                        print("Permission is already granted.");
                      }else if(status.isPermanentlyDenied){
                        //permission is permanently denied.
                        print("Permission is permanently denied");
                      }else if(status.isRestricted){
                        //permission is OS restricted.
                        print("Permission is OS restricted.");
                      }
                   },
                 ),
               )
           ],
         ),
      )
    );
  }
}

Output Screenshots:

UI output Request Popup

In this way, you can request Permission in Flutter App.

No any Comments on this Article


Please Wait...