Repository navigation
Replies: 1 comment
|
In 0.80.5, So the way to keep the UI alive is to catch the exception inside the handler. Rather than adding import functools, inspect
import flet as ft
def safe(handler):
@functools.wraps(handler)
async def wrapper(*args, **kwargs):
try:
result = handler(*args, **kwargs)
if inspect.isawaitable(result):
await result
except Exception as ex:
ft.context.page.show_dialog(ft.SnackBar(ft.Text(f"Something went wrong: {ex}")))
return wrapper
@ft.component
def App():
state, _ = ft.use_state(AppState(counter=0))
return [
ft.ProgressBar(state.counter),
ft.Button("Run!", on_click=safe(state.start_counter)),
]
If you'd rather show a dialog, use |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Question
Hi all,
I'm developing an async desktop application with Flet 0.80.5 on Windows 10 (Python 3.13.1), and I'm stuck on handling exceptions triggered in the
on_clickevent of Flet controls (e.g. ElevatedButton, TextButton).Current Problem
When an unhandled exception occurs inside the async
on_clickcallback function, the app directly pops up an exception traceback window. The only way to continue using the app is to close it and restart it, and the UI can't stay in its original state – this creates a very poor user experience.What I've Tried (But All Failed)
page.on_error– cannot catch exceptions fromon_clickcallbacks at all.on_clickexceptions:My Questions
on_click?Env Info
Any solutions, code examples, or hints would be greatly appreciated! Thanks a lot for your help 😊
Code sample
Error message
------------------------------------------------------
All reactions