flet-dev / flet-dev/flet

client_storage.get_async does'nt work on on_connect callback

Open
#3,613 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug has reproducible steps
Dominant language
Python
Stars
17k
Forks
694
Avg merge
1d 14h
Merged PRs (30d)
33

Description

Duplicate Check
Describe the bug

page.client_storage.get_async does'n work when called from page.on_connect callback.
Here is a sample code:

import flet
import flet as ft
from flet import (
    Page,
    colors
)


async def on_connect_callback(e):

    try:
        await e.page.client_storage.get_async('token')
    except Exception as e:
        print(e)


async def main(page: Page):
    print("ddd")
    page.on_connect = on_connect_callback
    page.title = "Flet Trello clone"
    page.padding = 0
    page.bgcolor = colors.BLUE_GREY_200
    page.views.clear()
    page.views.append(flet.View(

        appbar=flet.AppBar(title=flet.Text("sdf"), bgcolor=colors.GREEN)
    ))

    await page.update_async()

In the code I contained the get_async call in a try block and I printed the resulted exception and it says:
**Timeout waiting for invokeMethod clientStorage:get({'key': 'token'}) call
**

I ran the code with fastapi like this:

from fastapi import FastAPI, Request, WebSocket
from fastapi.responses import RedirectResponse

from api.common.db.sql import engine
from api.auth import router as auth_router
from api.config import BASE_DIR
from sqlmodel import SQLModel
import flet_fastapi
from ui.main import main as main_ui

app = FastAPI()

@app.on_event("startup")
async def startup():
    SQLModel.metadata.create_all(engine)
    pass

flet_fastapi_app = flet_fastapi.app(main_ui)
app.mount('/ui', flet_fastapi_app) # this is where we mount the felt to fastapi
#
app.include_router(
    router=auth_router,
    prefix='/auth'
)
@app.get('/')
async def home():
    return RedirectResponse('/ui')

command: uvicorn main:app --reload
But I wanted to check if it happens when running with felt only. so, I created a test.py file and comply the ui/main.py UI code and then:
command: felt test.py --web
This time it doesn't show any error.

Code sample
Logs
import flet
import flet as ft
from flet import (
    Page,
    colors
)


async def on_connect_callback(e):

    try:
        await e.page.client_storage.get_async('token')
    except Exception as e:
        print(e)


async def main(page: Page):
    print("ddd")
    page.on_connect = on_connect_callback
    page.title = "Flet Trello clone"
    page.padding = 0
    page.bgcolor = colors.BLUE_GREY_200
    page.views.clear()
    page.views.append(flet.View(

        appbar=flet.AppBar(title=flet.Text("sdf"), bgcolor=colors.GREEN)
    ))

    await page.update_async()

from fastapi import FastAPI, Request, WebSocket
from fastapi.responses import RedirectResponse

import flet_fastapi

app = FastAPI()



flet_fastapi_app = flet_fastapi.app(main_ui)
app.mount('/ui', flet_fastapi_app) # this is where we mount the felt to fastapi
#
app.include_router(
    router=auth_router,
    prefix='/auth'
)
@app.get('/')
async def home():
    return RedirectResponse('/ui')
To reproduce

/ represents the root of the project

  1. copy the main_ui code into a separate file /ui/main.py
  2. copy the main.py code /main.py
  3. copy the test.py code into /test.py
  4. run main.py with uvicorn main:app --reload for running the felt with fastapi
  5. run test.py with flet test.py --web for running with flet
Expected behavior

No response

Screenshots / Videos
Screenshots / Video demonstration

[Upload media here]

Operating System

Linux

Operating system details

Debian GNU/Linux 12 (bookworm)

Flet version

0.19.0

Regression

I'm not sure / I don't know

Suggestions

No response

Logs
Logs
[Paste your logs here]
Additional details

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ui/main.py and the page.on_connect callback, then compare the FastAPI-mounted flet_fastapi path in main.py with the working test.py --web path. Reproduce the client-storage timeout using uvicorn and establish the expected behavior for get_async during on_connect, with coverage for that case when it is clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.