Repository navigation
QuestionHello i try to migrate my apps from flet 0.28.3 to 0.80.1. i found the new docs with a new basic example but all that covers only async apps. How can i deal with it in non async apps ? Code sampleimport 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)Error messageNo response ------------------------------------------------------
|
Replies: 1 comment
|
Flet is now moving towards a single-threaded async model (like in JavaScript or Flutter). Therefore, I think it’s better to adapt your code to use Alternatively, you could try managing threads yourself, using a more complex approach, for example: from concurrent.futures import Future
import flet as ft
def main(page: ft.Page):
prefs = ft.SharedPreferences()
def set_value(e):
page.run_task(prefs.set, store_key.value, store_value.value)
page.show_dialog(ft.SnackBar("Value saved"))
def get_value(e):
future = page.run_task(prefs.get, get_key.value)
future.add_done_callback(
lambda f: page.run_thread(
lambda: update_value(f)
)
)
def update_value(f: Future):
try:
get_inp.value = f.result()
page.update()
except Exception:
page.show_dialog(ft.SnackBar("Error"))
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"),
get_inp := ft.TextField(label="Value"),
ft.Button("Get", on_click=get_value),
]
),
]
)
)
ft.run(main)I don’t think you really need to go this route. Managing threads and the associated logic adds complexity as your project grows. Besides being harder to maintain, it can also negatively impact performance https://flet.dev/blog/introducing-flet-1-0-alpha/#single-threaded-async-ui-model |
Flet is now moving towards a single-threaded async model (like in JavaScript or Flutter). Therefore, I think it’s better to adapt your code to use
async, as it’s simpler and safer.Alternatively, you could try managing threads yourself, using a more complex approach, for example: