When trying to use asynchronous code in environments that already have an event loop running you encounter the error: RuntimeError: asyncio.run() cannot be called from a running event loop
Your best bet is to modify the script:
import asyncio
import flet as ft
async def main(page: ft.Page):
page.title = "Flet counter example"
page.vertical_alignment = ft.MainAxisAlignment.CENTER
txt_number = ft.TextField(value="0", text_align=ft.TextAlign.RIGHT, width=100)
def minus_click(e):
txt_number.value = str(int(txt_number.value) - 1)
page.update()
def plus_click(e):
txt_number.value = str(int(txt_number.value) + 1)
page.update()
page.add(
ft.Row(
[
ft.IconButton(ft.icons.REMOVE, on_click=minus_click),
txt_number,
ft.IconButton(ft.icons.ADD, on_click=plus_click),
],
alignment=ft.MainAxisAlignment.CENTER,
)
)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
if loop.is_running():
print("Can't run Flet because there's already a running event loop 😢")
else:
loop.run_until_complete(ft.app(target=main))
If it doesn't work you should try running your script outside the IDE.
asyncio.run()is called directly inside theft.app()function, which is causing the error because it's already running within an event loop. You can run the Flet app inside a separate event loop.