Skip to content

client_storage.get_async does'nt work on on_connect callback #3613

Open
@abdulkuddusa4

Description

@abdulkuddusa4

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions