Overview
Jaspr provides several components for handling asynchronous operations:FutureBuilder- Builds UI based on aFutureStreamBuilder- Builds UI based on aStreamAsyncStatelessComponent- Server-only component with async build methodAsyncBuilder- Server-only wrapper for async builder functions
FutureBuilder
Component that builds itself based on the latest snapshot of interaction with aFuture.
Constructor
The asynchronous computation to which this builder is currently connected. Must be obtained earlier (e.g., in
initState), not created during build.The data that will be used to create the initial snapshot. Ensures the first frame shows useful data.
The build strategy. Receives an
AsyncSnapshot<T> and returns a Component.An optional key for the component.
Example Usage
With Initial Data
Server-Safe Pattern
StreamBuilder
Component that builds itself based on the latest snapshot of interaction with aStream.
Constructor
The asynchronous stream to which this builder is currently connected.
The data that will be used to create the initial snapshot.
The build strategy. Receives an
AsyncSnapshot<T> and returns a Component.An optional key for the component.
Example Usage
AsyncSnapshot
Immutable representation of the most recent interaction with an asynchronous computation. Generic typeAsyncSnapshot<T> where T is the data type.
Properties
Current state of connection to the asynchronous computation.
The latest data received by the asynchronous computation.
The latest error object received by the asynchronous computation.
The latest stack trace object received by the asynchronous computation.
Methods
hasData
data value.
hasError
error value.
requireData
error if hasError, throws StateError if neither hasData nor hasError.
ConnectionState
Enum representing the state of connection to an asynchronous computation.Not currently connected to any asynchronous computation.
Connected to an asynchronous computation and awaiting interaction.
Connected to an active asynchronous computation (e.g., a Stream that has returned at least one value).
Connected to a terminated asynchronous computation.
AsyncStatelessComponent
Server-only component. This component is only permitted to be used on the server.
build method.
Constructor
Methods
build
Future<Component>.
The build context.
A future that completes with the component to render.
Example Usage
AsyncBuilder
Server-only component. This component is only permitted to be used on the server.
Builder component. Accepts an async builder function that returns a future of a component.
Constructor
Async function that builds the component.
An optional key for the component.
Example Usage
Multiple Async Operations
Best Practices
Don't create futures in build
Don't create futures in build
Always create futures outside of the build method, such as in
initState for stateful components.Use server-only async components for SSR
Use server-only async components for SSR
For server-side rendering with async data, use
AsyncStatelessComponent or PreloadStateMixin instead of FutureBuilder.Handle all connection states
Handle all connection states
Always handle
waiting, done, error, and data states in your builder.Provide initial data when possible
Provide initial data when possible
Use
initialData to ensure the first frame shows meaningful content.Related Components
- StatefulComponent - For components with mutable state
- StatelessComponent - For components without mutable state