Last updated:
0 purchases
get it future builder
Get It Future Builder #
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:
copied to clipboard
Install it:
flutter packages get
copied to clipboard
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
);
copied to clipboard
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}'),
)
copied to clipboard
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
);
copied to clipboard
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}',
),
),
copied to clipboard
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,
// ...
)
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.