flet icon indicating copy to clipboard operation
flet copied to clipboard

[bug (right?)] self.page is noneType when build is called in UserControl

Open ItsCubeTime opened this issue 3 years ago • 3 comments

import flet

class userControl(flet.UserControl):
    def build(self):
        print(type(self))
        self.page.padding = 0

def main(page: flet.Page):
    page.add(userControl())

flet.app(target=main)

Returns:

Unhandled error processing page session c966d129-8aa3-450c-983e-af7f8ee29d8d: Traceback (most recent call last):
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\flet.py", line 212, in on_session_created
    session_handler(page)
  File "c:\Users\olliv\Desktop\Art And Development\Flutter Learning\Flet\Calculator example\test7.py", line 9, in main
    page.add(userControl())
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\page.py", line 229, in add
    return self.__update(self)
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\page.py", line 203, in __update
    control.build_update_commands(self._index, added_controls, commands)
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\control.py", line 391, in build_update_commands
    innerCmds = ctrl._build_add_commands(
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\control.py", line 439, in _build_add_commands
    childCmd = control._build_add_commands(
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\control.py", line 419, in _build_add_commands
    self._build()
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\user_control.py", line 12, in _build
    content = self.build()
  File "c:\Users\olliv\Desktop\Art And Development\Flutter Learning\Flet\Calculator example\test7.py", line 6, in build
    self.page.padding = 0
AttributeError: 'NoneType' object has no attribute 'padding

Would be quite useful to be able to access the page of the control in the build method, I'm assuming its intended to be accessible?

ItsCubeTime avatar Oct 16 '22 17:10 ItsCubeTime

Use UserContro.did_mount to access page:

import flet

class userControl(flet.UserControl):
    def build(self):
        print(type(self))
        
    def did_mount(self):
        self.page.padding = 0
        self.page.update()

def main(page: flet.Page):
    page.add(userControl())

flet.app(target=main)

FeodorFitsner avatar Oct 16 '22 17:10 FeodorFitsner

I see 👍

Tried making a working example:

import flet
from flet import Container, IconButton, Page, Row, Text, WindowDragArea, colors, icons


class userControl(flet.UserControl):
    def build(self):
        print(type(self))
        return WindowDragArea(content=flet.Stack(height=100000000000, width=100000000000),height=100000000000, width=100000000000)
        
    def did_mount(self):
        self.page.padding = 0
        print(f"Page padding directly after mod: {self.page.padding}")
        self.page.update()
        print("this print is never reached?")

def main(page: Page):
    page.window_title_bar_hidden = True
    page.window_title_bar_buttons_hidden = True

    page.add(
        userControl()
    )
    print("this print is never reached?")
    
flet.app(target=main)

However it seems it gets stuck on line 13 self.page.update() .

It would however also be kinda nice if one could get info about the page directly in build, like page.width to be able to customize the looks of the control based on it (for instance you may want a widget to be able to make itself skinnier & more compact if the page width is relatively low, or maybe hide certain data if theres too little space on the page) - even if did_mount would have solved this particular scenario.

ItsCubeTime avatar Oct 16 '22 18:10 ItsCubeTime

https://discord.com/channels/981374556059086931/1000264818604904499/1031572152190771271

Same issue on discord

ndonkoHenri avatar Oct 17 '22 15:10 ndonkoHenri

Closing in favor of https://github.com/flet-dev/flet/issues/489

FeodorFitsner avatar Oct 18 '22 20:10 FeodorFitsner