get_it_future_builder 0.3.4 copy "get_it_future_builder: ^0.3.4" to clipboard
get_it_future_builder: ^0.3.4 copied to clipboard

Wait for your async `get_it` dependencies to be ready in the presentation layer.

Get It Future Builder #

style: very good analysis License: MIT Pub package

get_it_future_builder provides a widget named GetItFutureBuilder to wait for your async dependencies to be ready on your presentation layer.

Installation 💻 #

❗ In order to start using Get It Future Builder you must have the Flutter SDK installed on your machine.

Add get_it_future_builder to your pubspec.yaml:

dependencies:
  get_it_future_builder:

Install it:

flutter packages get

How to Use It #

A Single Dependency #

You can also see example directory.

Register your async dependency through GetIt as such:

GetIt.I.registerSingletonAsync<Directory>(
  () async {
    return await getApplicationDocumentsDirectory();
  },
  instanceName: 'documents_dir', // optional
);

In our example, we'll use getApplicationDocumentsDirectory method of path_provider. Since it calls native code through MethodChannel, it has to be asynchronous.

Then, in the presentation layer, you can simply do:

// notice how we get the dependency with generics
GetItFutureBuilder<Directory>(
  // optional
  instanceName: 'documentsDir',
  // render this widget while loading
  loading: (context) => LinearProgressIndicator(),
  // render this widget when it's ready
  ready: (context, instance) => Text('documents dir is: ${instance.path}'),
)

Multiple Dependencies #

get_it_future_builder supports the initialization widget up to 3 dependencies. These widgets are respectively named:

  • GetItFutureBuilder2
  • GetItFutureBuilder3

To initialize 2 dependencies on your widget tree, use GetItFutureBuilder2.

Let's assume we have two asynchronous dependencies.

GetIt.I.registerSingletonAsync<Directory>(
  () async {
    return await getApplicationDocumentsDirectory();
  },
  instanceName: 'documents_dir', // optional
);

GetIt.I.registerSingletonAsync<Directory>(
  () async {
    return await getTemporaryDirectory();
  },
  instanceName: 'temp_dir', // optional
);

To initialize them asynchronously, use GetItFutureBuilder2 as below:

GetItFutureBuilder2<Directory, Directory>(
  instanceName1: 'documents_dir', // optional
  instanceName2: 'temp_dir', // optional
  loading: (context) => const LinearProgressIndicator(),
  ready: (context, instance1, instance2) => Text(
    'Documents dir is ${instance1.path} and temp dir is ${instance2.path}',
  ),
),

The initialization of dependencies run concurrently.

Different Service Locator #

By default, GetItFutureBuilder uses the global service locator GetIt.I (or GetIt.instance) to resolve dependencies. If you have initialized a different service locator, you can pass it to GetItFutureBuilder as below:

GetItFutureBuilder(
  // ...
  locator: myLocator,
  // ...
)
1
likes
150
pub points
11%
popularity

Publisher

verified publishererayerdin.com

Wait for your async `get_it` dependencies to be ready in the presentation layer.

Repository (GitHub)
View/report issues

Topics

#widget

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

flutter, get_it

More

Packages that depend on get_it_future_builder