Skip to content

Conversation

FeodorFitsner
Copy link
Contributor

@FeodorFitsner FeodorFitsner commented Sep 7, 2022

A new Flet API for storing key-value data on a client side in a persistent storage. Flet implementation uses shared_preferences Flutter package.

Writing data to the storage:

# strings
page.client_storage.set("key", "value")

# numbers, booleans
page.client_storage.set("number.setting", 12345)
page.client_storage.set("bool_setting", True)

# lists
page.client_storage.set("favorite_colors", ["read", "green", "blue"])

Each Flutter application using shared_preferences plugin has its own set of preferences. As the same Flet client (which is a Flutter app) is used to run UI for muliple Flet apps any values stored in one Flet application are visible/available to another Flet app running by the same user.

To distinguish one application settings from another it is recommended to use some unique prefix for all storage keys, for example {company}.{product}.. For example to store auth token in one app you could use acme.one_app.auth_token key and in another app use acme.second_app.auth_token.

It is responsibility of Flet app developer to encrypt sensitive data before sending it to a client storage, so it's not read/tempered by another app or an app user.

Reading data:

# The value is automatically converted back to the original type
value = page.client_storage.get("key")

colors = page.client_storage.set("favorite_colors")
# colors = ["read", "green", "blue"]

Check if a key exists:

page.client_storage.contains_key("key") # True if the key exists

Get all keys:

page.client_storage.get_keys("key-prefix.")

Remove a value:

page.client_storage.remove("key")

Clear the storage:

page.client_storage.clear()

clear() is a dangerous function that removes all preferences of all Flet apps ever run by the same user and serves as a heads-up that permanent application data shouldn't be stored in the client storage.

@FeodorFitsner FeodorFitsner merged commit 000c123 into main Sep 8, 2022
@FeodorFitsner FeodorFitsner deleted the client-storage branch September 8, 2022 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant