How To Ask For Permission Using Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 27-11-2020

Getting permissions from users is somewhat important task when we use differnt differnt functionality and for execute application in proper way else in some times it gives Exceptions.

How To Ask For Permission Using Flutter Android App

 Ask For Permission
Step 1 
We cannot directly remove the time stamp from  Ask For Permission  but using the intl.dart package we can easily filter the date stamp from time stamp. So open your flutter project’s pubspec.yaml in code .

dependencies:
  flutter:
    sdk: flutter
  permission_handler: ^3.0.1

Step 2
After done saving the pubspec.yaml file, Open your flutter project root folder in Command Prompt or Terminal and execute flutter pub get command.

flutter pub get



Complete Code For  Ask For Permission In Flutter
main.dart
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.");
                      }
                   },
                 ),
               )
           ],
         ),
      )
    );
  }
}





 

Related Post