...

/

The Get.putAsync Method

The Get.putAsync Method

Learn to use Get.putAsync to initialize dependencies asynchronously.

Initializing dependencies asynchronously

At times, our dependency results from an asynchronous operation, such as when using SharedPreferences (a simple key-value storage system used for persisting primitive data types in Flutter applications). We can perform the operation and initialize the dependency using the Get.putAsync method.

Press + to interact
Get.putAsync<SharedPreferences>(() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('name', 'Aachman');
return prefs;
});

Get.putAsync takes the builder parameter, which is a function that, when called, initiates an asynchronous process to obtain the dependency instance. The builder function returns a Future representing this asynchronous operation, which will eventually resolve to the dependency instance once the process is complete.

As with all other methods, we retrieve the dependency using Get.find:

Press + to interact
Get.find<SharedPreferences>().getString('name');
...