Future Builder

The Future Builder widget is used to build a widget based on the result of a Future (an asynchronous operation). This is essential for handling data from APIs or other long-running tasks, allowing you to display different widgets for loading, error, and success states.

Properties

Property
Description

future

The asynchronous operation to run. This is typically an API call or a delay.

initialData

The initial data to use while the future is resolving.

controller

An expression to control the future's execution.

onSuccess

An action to trigger when the future completes successfully.

onError

An action to trigger if the future completes with an error.

Future Configuration

Property
Description

futureType

The type of future to execute (e.g., delay, api).

durationInMs

If the futureType is delay, this is the duration of the delay in milliseconds.

dataSource

If the futureType is api, this defines the API endpoint to call.

Child of Future Builder

Slot
Description

child

The widget to build based on the state of the future. This child widget will have access to variables like futureState (loading, error, completed) and response.


Best Practices

  • Use the initialData property to provide a good user experience while the future is resolving.

  • Use a ConditionalBuilder as the child of the FutureBuilder to display different UI for different stream states (e.g., loading, error, data).

Last updated