How can I fix “Control must be added to the page first.” Error while using GestureDetector in flet?

I am trying to make a janggi(chess variant) game using flet. I wanted to use drag and drop for the pieces, so I followed the Solitaire tutorial on the flet docs. The problem occured when I tried to do the “move_on_top” part. Before adding the move_on_top code, things worked perfectly as expected. I could move the pieces around freely. However after I added the move_on_top function, It works sometimes, but it was also causing the AssertionError randomly, and the drag is not working anymore.

Future exception was never retrieved
future: <Future finished exception=AssertionError('Control must be added to the page first.')>
Traceback (most recent call last):
  File "C:UsersandypAppDataLocalProgramsPythonPython312Libconcurrentfuturesthread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersandypAppDataLocalProgramsPythonPython312Libsite-packagesflet_corepage.py", line 950, in wrapper
    handler(*args)
  File "c:limbaksajanggi-v2janggiBoard.py", line 22, in drag
    e.control.update()
  File "C:UsersandypAppDataLocalProgramsPythonPython312Libsite-packagesflet_corecontrol.py", line 314, in update
    assert self.__page, "Control must be added to the page first."
           ^^^^^^^^^^^
AssertionError: Control must be added to the page first.

I thought that this had to do with move_on_top function. I think the error is occuring when I try to update them after the function remove and re-append the piece to the controls. But I do not understand why this is happening since the function adds the piece back into the controls after it removes it. My code is here, I simplified it as much as I could. There are 90 invisible slots, and pieces are placed on the slots according to the board. The janggibase.Board has informations about the board such as the list of pieces on the board. The janggibase.Piece contains informations about the piece such as it’s location. I also uploaded the full code to [my github](github.com/limbaksa/janggi-v2/)

import flet as ft
import janggibase

def move_on_top(piece,controls):
    controls.remove(piece)
    controls.append(piece)
    piece.board.update()

def start_drag(e:ft.DragStartEvent):
    move_on_top(e.control,e.control.board.controls)
    e.control.board.move_start_top=e.control.top
    e.control.board.move_start_left=e.control.left

def bounce_back(board,piece):
    piece.top=board.move_start_top
    piece.left=board.move_start_left
    board.update()

def drag(e: ft.DragUpdateEvent):
   e.control.top = max(0, e.control.top + e.delta_y)
   e.control.left = max(0, e.control.left + e.delta_x)
   e.control.update()

def drop(e:ft.DragEndEvent):
    for slot in e.control.board.slots:
        if (
            abs(e.control.top - slot.top) < 20
            and abs(e.control.left - slot.left) < 20
        ):
            place(e.control, slot)
            break
    else:
        bounce_back(e.control.board,e.control)
    e.control.update()

def place(piece,slot):
        piece.top=slot.top
        piece.left=slot.left
        piece.board.update()


class janggiPiece(ft.GestureDetector):
    def __init__(self, piece: janggibase.Piece, board: "janggiBoard"):
        super().__init__()
        self.piece=piece
        self.board=board
        self.mouse_cursor=ft.MouseCursor.MOVE
        self.drag_interval=5
        self.on_pan_start=start_drag
        self.on_pan_update=drag
        self.on_pan_end=drop
        self.left = 60 * (piece.location // 10)
        self.top = 60 * (9 - piece.location % 10)
        self.content=ft.Container(
            width=60,
            height=60,
            border_radius=ft.border_radius.all(5),
            content=ft.Image(f"img/{str(piece).upper()}{piece.color}.png"),
        )
    



class Slot(ft.Container):
    def __init__(self, top, left):
        super().__init__()
        self.ontop = None
        self.width = 60
        self.height = 60
        self.left = left
        self.top = top


class janggiBoard(ft.Stack):
    def __init__(self, board: janggibase.Board):
        super().__init__()
        self.board = board
        self.slots = []
        self.controls = []
        self.width = 540
        self.height = 600
        for i in range(90):
            self.slots.append(Slot(60 * (9 - i % 10), 60 * (i // 10)))
        self.controls.extend(self.slots)
        self.piecelist = []
        for color in range(2):
            for piece in self.board.pieces[color]:
                self.piecelist.append(janggiPiece(piece, self))
        self.controls.extend(self.piecelist)
        self.move_start_top=None
        self.move_start_left=None


if __name__ == "__main__":
    def main(page):
        page.add(janggiBoard(janggibase.Board(15)))

    ft.app(target=main)

I also tried to switch the move_on_top function as below so that the piece never gets remoed from the controls, but I still got the same error.

def move_on_top(piece,controls:list):
    i=controls.index(piece)
    controls[i],controls[-1]=controls[-1],controls[i]
    piece.board.update()

New contributor

limbaksa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

I had a looked at your code in the linked repo.
The main issue is that, in your helper methods you update the board instead of updating the piece. I made just the below 2 modifications to the code in your linked repo (janggiBoard.py) and it worked again:

    def bounce_back(board, piece):
        # ...
        piece.update()

    def place(piece, slot):
        # ...
        piece.update()

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật