flet icon indicating copy to clipboard operation
flet copied to clipboard

Setting adaptive=True on Mac causes SnackBar and Banner to fail with ```Null check operator used on a null value```.

Open lc0rp opened this issue 1 year ago • 0 comments

Description

For the past week I was trying to figure out why my Banners and SnackBars weren't working. It turns out that on my setup (Mac OS Sonoma), adding page.adaptive=True causes these widgets to fail with Null check operator used on a null value.

The examples and error logs are pasted below.

Code example to reproduce the issue:

The SnackBar example from the docs, with the pesky line added as a comment.

import logging

import flet as ft

logging.basicConfig(level=logging.DEBUG)


class Data:
    def __init__(self) -> None:
        self.counter = 0


d = Data()


def main(page):
    # page.adaptive = True # <==== THIS LINE CAUSES IT TO FAIL. NO SNACKBAR IS SHOWN.
    page.snack_bar = ft.SnackBar(
        content=ft.Text("Hello, world!"),
        action="Alright!",
    )

    def on_click(e):
        page.snack_bar = ft.SnackBar(ft.Text(f"Hello {d.counter}"))
        page.snack_bar.open = True
        d.counter += 1
        page.update()

    page.add(ft.ElevatedButton("Open SnackBar", on_click=on_click))


ft.app(target=main)

The Banner example from the docs, with the pesky line added as a comment.

import flet as ft

def main(page):
    def close_banner(e):
        page.banner.open = False
        page.update()

    # page.adaptive = True # <==== THIS LINE CAUSES IT TO FAIL. NO BANNER IS SHOWN.
    page.banner = ft.Banner(
        bgcolor=ft.colors.AMBER_100,
        leading=ft.Icon(ft.icons.WARNING_AMBER_ROUNDED, color=ft.colors.AMBER, size=40),
        content=ft.Text(
            "Oops, there were some errors while trying to delete the file. What would you like me to do?"
        ),
        actions=[
            ft.TextButton("Retry", on_click=close_banner),
            ft.TextButton("Ignore", on_click=close_banner),
            ft.TextButton("Cancel", on_click=close_banner),
        ],
    )

    def show_banner_click(e):
        page.banner.open = True
        page.update()

    page.add(ft.ElevatedButton("Show Banner", on_click=show_banner_click))

ft.app(target=main)

Describe the results you received:

When page.adaptive=True, instead of displaying the SnackBar or Banner, the buttons do nothing in the UI.

Describe the results you expected:

When page.adaptive=False or not included in the code, the buttons will show the Banner or SnackBar on_click(..)

Additional information you deem important (e.g. issue happens only occasionally):

When logging is enabled, the error shown is Null check operator used on a null value thrown by the page error handler.

DEBUG:flet_runtime:_on_message: {"action":"pageEventFromWeb","payload":{"eventTarget":"_2","eventName":"click","eventData":""}}
DEBUG:flet_core:page.on_event_async: _2 click 
DEBUG:flet_core:_process_command: set ['_4'] {'open': 'true'}
DEBUG:flet_runtime:__send: {"action":"pageControlsBatch","payload":[{"action":"updateControlProps","payload":{"props":[{"i":"_4","open":"true"}]}}]}
DEBUG:flet_runtime:sent to TCP: 125
DEBUG:flet_runtime:_on_message: {"action":"pageEventFromWeb","payload":{"eventTarget":"page","eventName":"error","eventData":"Null check operator used on a null value"}}
DEBUG:flet_core:page.on_event_async: page error Null check operator used on a null value

Flet version (pip show flet):

Name: flet
Version: 0.22.0
Summary: Flet for Python - easily build interactive multi-platform apps in Python
Home-page: 
Author: Appveyor Systems Inc.
Author-email: [email protected]
License: Apache-2.0
Location: /XXXX/.venv/lib/python3.12/site-packages
Requires: cookiecutter, fastapi, flet-runtime, packaging, qrcode, uvicorn, watchdog
Required-by: 

Give your requirements.txt file (don't pip freeze, instead give direct packages):

Relevant parts of pyproject.toml:

...
[tool.poetry.dependencies]
python = ">=3.11,<3.13"
flet = "0.22.0"
nltk = "^3.8.1"
pydantic = "^2.6.3"
pydantic-settings = "^2.2.1"
instructor = "^0.6.4"
aiofiles = "^23.2.1"
aiosqlite = "^0.20.0"

[tool.poetry.group.dev.dependencies]
pytest = "^8.0.2"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

Hardware and Operating system:

PC: Macbook Pro 16 inch 2023 Chip: Apple M2 Pro OS: macOS Sonoma 14.3.1 (23D60)

lc0rp avatar Apr 12 '24 18:04 lc0rp