Is it possible to save data in an application after restarting the application? Python, Flet

65 Views Asked by At

I created a to-do list type app. In the application, the user can create his own new task. However, after closing the application, not all tasks were saved. Can this be fixed by authenticating/registering each user to save data or something? How can I fix this? Same problem with the web

I'm new to programming and don't know how to change this.

2

There are 2 best solutions below

1
Maxim Kollar On

In application, you can use an .txt file to store data.

Or you can create another python file which will be 1 big dictionary with users, it can be named users.py. When someone is registered, you will simply add one's name into dictionary and this account will have a list of strings which will contain all to-dos. Then, when the user logs in account, you will read all data from this list and display it on the screen.

An alternative for this is to use JSON file, but you will have to import JSON module.

0
JAS On

if you are saying save before exit or restart. yes you can! just take a look in https://flet.dev/docs/guides/python/client-storage

simple:

# 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", ["red", "green", "blue"])

but caution with one thing:

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.