Fetch data on route entry
Recipe for load() and fallback().
Fetch data on route entry
Problem: a screen needs remote data before it can render anything useful.
Steps:
- Override
loadand perform the fetch there. It runs on the Tokio pool, never on the UI thread:
async fn load(&mut self) -> anyhow::Result<()> {
self.service = fetch_service().await?;
Ok(())
}
- Override
fallbackwith what shows whileloadis pending:
fn fallback(&self) -> Element {
Text::new("Loading service…").into_element()
}
-
Enter the route. The framework renders
fallbackon the first frame and swaps inview()onceloadresolves — with no flicker in between. -
If the user navigates away mid-load, the task is cancelled. Nothing dispatches into the abandoned view.
Variants: return Err from load to keep showing fallback; retry by
re-entering the route. Keep load idempotent — entering the same route twice
must be safe.