---
title: AsyncBuilder and AsyncStatelessComponent
descriptions: Server-side components with an async build method.
previous: /components/raw_text
next: /components/router
---

---

The `AsyncBuilder` and `AsyncStatelessComponents` are special server-side components that
can have an async build method that lets you do any asynchronous work before rendering the component.

<Warning>
These components can only be used on the server and are only available using the
`package:jaspr/server.dart` import.
</Warning>

Typically during pre-rendering, you have some additional data you want to load and display as part
of the pre-rendered html. If this data is loaded asynchronously (e.g. from a database, network request etc.)
you need to **delay the build phase** until all data is loaded.

## Usage

The async components allow you to put your data-loading logic directly into the build method.

- The `AsyncBuilder` component takes an async `builder: Stream<Component> Function(BuildContext context)` parameter.
- The `AsyncStatelessComponent` has an abstract `Stream<Component> build(BuildContext context)` method.

Both methods are designed to resemble an async variant of the standard `Iterable<Component> Function(BuildContext context)`
build method. Similarly, it is supposed to be used with the **async\* / yield** syntax:

```dart
class MyAsyncComponent extends AsyncStatelessComponent {

  @override
  Stream<Component> build(BuildContext context) async* {
    // Simply use "await" inside the build method.
    var data = await loadDataFromDatabase();

    // Renders the component after the data has loaded.
    yield MyOtherComponent(data: data);
  }
}
```

or using the `AsyncBuilder` component:

```dart
yield AsyncBuilder(builder: (context) async* {
  // Simply use "await" inside the build method.
  var data = await loadDataFromDatabase();

  // Renders the component after the data has loaded.
  yield MyOtherComponent(data: data);
});
```

When using these components and performing async work, the pre-rendering of **the whole page will be delayed** until all async work is done.
While this is less of a concern in static mode, be aware that in server mode this can potentially **affect the load time of your website**.

<Warning>
As a Flutter developer, it may feel very strange or dangerous to use `async/await` as part of the build method for a component.
Be assured that this is perfectly safe, mainly for the reason that this component is **only executed on the server** and
**only built once** during pre-rendering. Therefore, you don't need to worry about potentially expensive rebuilds or floating async
operations with these components.
</Warning>

---

## Alternatives

- Check out the [PreloadStateMixin](/utils/preload_state_mixin) for an alternative way to load async data on the server
  as part of a `StatefulComponent`.
