Skip to content

SharedPreferences

Provides access to persistent key-value storage.

Inherits: Service

Methods

  • clear

    Clears all keys and values.

  • contains_key

    Checks if the given key exists.

  • get

    Gets the value for the given key.

  • get_keys

    Gets all keys with the given prefix.

  • remove

    Removes the value for the given key.

  • set

    Sets a value for the given key.

Examples#

Basic Example#

import flet as ft


async def main(page: ft.Page):
    async def set_value():
        await ft.SharedPreferences().set(store_key.value, store_value.value)
        get_key.value = store_key.value
        store_key.value = ""
        store_value.value = ""
        page.show_dialog(ft.SnackBar("Value saved to SharedPreferences"))

    async def get_value():
        contents = await ft.SharedPreferences().get(get_key.value)
        page.add(ft.Text(f"SharedPreferences contents: {contents}"))

    page.add(
        ft.Column(
            [
                ft.Row(
                    [
                        store_key := ft.TextField(label="Key"),
                        store_value := ft.TextField(label="Value"),
                        ft.Button("Set", on_click=set_value),
                    ]
                ),
                ft.Row(
                    [
                        get_key := ft.TextField(label="Key"),
                        ft.Button("Get", on_click=get_value),
                    ]
                ),
            ],
        )
    )


ft.run(main)

Methods#

clear async #

clear() -> bool

Clears all keys and values.

contains_key async #

contains_key(key: str) -> bool

Checks if the given key exists.

get async #

get(key: str)

Gets the value for the given key.

get_keys async #

get_keys(key_prefix: str) -> list[str]

Gets all keys with the given prefix.

remove async #

remove(key: str) -> bool

Removes the value for the given key.

set async #

set(key: str, value: Any) -> bool

Sets a value for the given key.