Exception: control with ID '_12' not found #827
-
I am creating simple App having two ElevatedButton buttons(expand and shrink) and ListView. What I want that whenever I click on expand button , ListView get expanded with child item ( Text("test") ) , and vice versa Expansion of ListView is working fine but not shrinking. Exception in thread Thread-14 (shrink): Below is my code
` |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
There's a really strange behavior here. import flet as ft
def main(page):
lview = ft.ListView(auto_scroll=True)
tile = ft.ListTile(title=ft.Text("Test"))
# lview.controls.append(tile)
# lview.controls.append(tile)
def shrink(e):
print(f"Before shrink: {lview.controls=}")
lview.controls.pop()
page.update()
print(f"After shrink: {lview.controls=}")
def expand(e):
print(f"Before expand: {lview.controls=}")
lview.controls.append(tile)
page.update()
print(f"After expand: {lview.controls=}")
page.add(
ft.Row(
[
ft.ElevatedButton("expand", on_click=expand),
ft.ElevatedButton("shrink", on_click=shrink)
]
),
lview
)
ft.app(target=main) |
Beta Was this translation helpful? Give feedback.
-
@FeodorFitsner, to avoid talking of the 'happy clicking' let's look/consider this simple code below. I wish to expose an issue (which you should have noticed if you ran the code I gave a while); import flet as ft
def main(page):
lview = ft.ListView(auto_scroll=True)
tile = ft.ListTile(title=ft.Text("Test"))
def expand(e):
print(f"Just Before Addition: {lview.controls=}")
lview.controls.append(tile)
page.update()
print(f"After Addition: {lview.controls=} \n")
page.add(
ft.Row(
[
ft.ElevatedButton("expand", on_click=expand),
]
),
lview
)
ft.app(target=main) Let me know what you think of this strange behavior. 🙂 |
Beta Was this translation helpful? Give feedback.
It's all happening because you are adding the same control to the page again. All controls on a page should have unique IDs. That ID is assigned when control is added to a page. When you add the same control over and over, by reference, all those references point to the same control with the same ID. A correct way is adding a new control instance every time you click the button: