Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
  • Flutter Apps
Menu
Example of how to use the permission_handler package in Flutter

Example of how to use the permission_handler package in Flutter

Posted on January 22, 2025January 28, 2025 by Toma Velev

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:

  1. isGranted: This indicates that the user has granted the permission to your app.
  2. isDenied: This means that the user has denied the permission to your app, either permanently or temporarily.
  3. 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.
  4. 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.
  5. isLimited: This status indicates that the user has granted a limited permission, which restricts some aspects of the permission.
  6. isRestricted: This status indicates that the user has restricted a permission, which means that some aspects of the permission are not available.
  • Example of GridView Builder in Flutter
  • How to Visualize Listview inside Listview in Flutter
  • What other usages you know about public private cryptography
  • Get a Flutter App to Production
  • Firebase Dynamic Links Deprecation – migrating out to Java

Categories

  • Apps (20)
  • ChatGPT (19)
  • Choosing a Framework (38)
  • Flutter (201)
  • Graphical User Interface (13)
  • Marketing (113)
  • Software Development (268)
  • Spring (41)
  • StartUp (21)
  • Uncategorized (15)
  • Uncategorized (4)
  • Vaadin (14)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (83)
    • Flutter Apps (23)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Example of GridView Builder in Flutter
  • How to Visualize Listview inside Listview in Flutter
  • What other usages you know about public private cryptography
  • Get a Flutter App to Production
  • Firebase Dynamic Links Deprecation – migrating out to Java

Post Categories

  • Apps (20)
  • ChatGPT (19)
  • Choosing a Framework (38)
  • Flutter (201)
  • Graphical User Interface (13)
  • Marketing (113)
  • Software Development (268)
  • Spring (41)
  • StartUp (21)
  • Uncategorized (15)
  • Uncategorized (4)
  • Vaadin (14)