Repository navigation
QuestionWhen I tried using a component function to build the control tree for a View object for screen transitions, I encountered the following runtime error. Is it not possible to use a component function in this situation? Code sampleimport flet as ft
@ft.component
def view_content():
return ft.Text('some settings...')
@ft.control
class SettingsView( ft.View ):
def __post_init__( self, *args, **kwargs ):
super().__post_init__( *args, **kwargs )
self.route = '/settings'
self.controls = [ view_content() ] # RuntimeError
@ft.component
def main():
page = ft.context.page
def route_change():
page.views.clear()
page.views.append( ft.View( route="/", controls = [ ft.Text('root view') ] ) )
if page.route == "/settings":
page.views.append( SettingsView() )
page.update()
page.on_route_change = route_change
page.go('/settings')
ft.run( lambda page: page.render( main ) )Error messageRuntimeError: No current renderer is set. Call via Renderer.render(...) or Renderer.with_context(...).------------------------------------------------------
|
Replies: 1 comment 1 reply
|
This happens because a For this particular example, the smallest fix is to make def view_content():
return ft.Text("some settings...")
@ft.control
class SettingsView(ft.View):
def __post_init__(self, *args, **kwargs):
super().__post_init__(*args, **kwargs)
self.route = "/settings"
self.controls = [view_content()]Use that version if If it does need to be a real component, make the views part of the declaratively rendered tree instead of constructing them in @ft.component
def ViewContent():
return ft.Text("some settings...")
@ft.component
def App():
route, set_route = ft.use_state(ft.context.page.route)
ft.context.page.on_route_change = lambda e: set_route(e.route)
return [
ft.View(route="/", controls=[ft.Text("root view")]),
*(
[ft.View(route="/settings", controls=[ViewContent()])]
if route == "/settings"
else []
),
]
ft.run(lambda page: page.render_views(App))The repository's current two-page declarative routing example uses this same pattern: the component returns the So the short answer is: a component can build a View's controls, but only when that View is itself produced inside the component render tree. Removing |
This happens because a
@ft.componentfunction is not a normal control factory. It must run while Flet's component renderer is reconciling a tree.SettingsView.__post_init__()is ordinary control construction, and in this example it is reached from the imperative route callback, so there is no current renderer; that is exactly what the exception is reporting.For this particular example, the smallest fix is to make
view_contenta plain factory: