Skip to content

PermissionHandler

Examples#

Example 1#

example_1.py
import flet as ft

import flet_permission_handler as fph


def main(page: ft.Page):
    page.appbar = ft.AppBar(title="PermissionHandler Playground")

    def show_snackbar(message: str):
        page.show_dialog(ft.SnackBar(ft.Text(message)))

    async def get_permission_status(e: ft.Event[ft.OutlinedButton]):
        status = await p.get_status(fph.Permission.MICROPHONE)
        show_snackbar(f"Microphone permission status: {status.name}")

    async def request_permission(e: ft.Event[ft.OutlinedButton]):
        status = await p.request(fph.Permission.MICROPHONE)
        show_snackbar(f"Requested microphone permission: {status.name}")

    async def open_app_settings(e: ft.Event[ft.OutlinedButton]):
        show_snackbar("Opening app settings...")
        await p.open_app_settings()

    p = fph.PermissionHandler()
    page.services.append(p)  # (1)!

    page.add(
        ft.OutlinedButton("Open app settings", on_click=open_app_settings),
        ft.OutlinedButton("Request Microphone permission", on_click=request_permission),
        ft.OutlinedButton(
            "Get Microphone permission status", on_click=get_permission_status
        ),
    )


ft.run(main)
  1. The PermissionHandler instance must be added to the [Page.services][flet.Page.services] list to work properly.

PermissionHandler #

Bases: Service

Manages permissions for the application.

This control is non-visual and should be added to [Page.services][flet.Page.services] list.

Platform support

Currently only supported on Android, iOS, Windows, and Web platforms.

RAISES DESCRIPTION
FletUnsupportedPlatformException

If the platform is not supported.

data #

data: Any = skip_field()

Arbitrary data of any type.

key #

key: KeyValue | None = None

page #

page: Page | BasePage | None

The page to which this control belongs to.

parent #

parent: BaseControl | None

The direct ancestor(parent) of this control.

It defaults to None and will only have a value when this control is mounted (added to the page tree).

The Page control (which is the root of the tree) is an exception - it always has parent=None.

before_event #

before_event(e: ControlEvent)

before_update #

before_update()

build #

build()

Called once during control initialization to define its child controls. self.page is available in this method.

did_mount #

did_mount()

get_status #

get_status(
    permission: Permission, timeout: int = 10
) -> PermissionStatus | None

Gets the current status of the given permission.

PARAMETER DESCRIPTION
permission

The Permission to check the status for.

TYPE: Permission

timeout

The maximum amount of time (in seconds) to wait for a response.

TYPE: int DEFAULT: 10

RETURNS DESCRIPTION
PermissionStatus | None

A PermissionStatus if the status is known, otherwise None.

RAISES DESCRIPTION
TimeoutError

If the request times out.

init #

init()

is_isolated #

is_isolated()

open_app_settings #

open_app_settings(timeout: int = 10) -> bool

Opens the app settings page.

PARAMETER DESCRIPTION
timeout

The maximum amount of time (in seconds) to wait for a response.

TYPE: int DEFAULT: 10

RETURNS DESCRIPTION
bool

True if the app settings page could be opened, otherwise False.

RAISES DESCRIPTION
TimeoutError

If the request times out.

request #

request(
    permission: Permission, timeout: int = 60
) -> PermissionStatus | None

Request the user for access to the permission if access hasn't already been granted access before.

PARAMETER DESCRIPTION
permission

The Permission to request.

TYPE: Permission

timeout

The maximum amount of time (in seconds) to wait for a response.

TYPE: int DEFAULT: 60

RETURNS DESCRIPTION
PermissionStatus | None

The new PermissionStatus after the request, or None if the request was not successful.

RAISES DESCRIPTION
TimeoutError

If the request times out.

update #

update() -> None

will_unmount #

will_unmount()