...

/

Storage Implementation

Storage Implementation

Learn to implement local storage in the app using GetStorage, a lightweight, synchronous storage solution.

Introduction to GetStorage

GetStorage is GetX’s take on local storage. It is fast, light, and synchronous. Above all, it’s available on every platform, including the web. This makes it an excellent alternative to SharedPreferences, which is an asynchronous, key-value-based storage solution. However, it lacks dedicated database features like query and cannot replace a local database like Hive. Keeping that in mind, let’s understand its syntax and features.

Initialization

First, we need to initialize the storage bucket.

Press + to interact
main() async {
await GetStorage.init();
runApp(App());
}

main is the right place to initialize because we want our local storage up and running before the app is launched.

Next, create an instance of the GetStorage class to access the bucket.

Press + to interact
final GetStorage box = GetStorage();

We can also initialize and access multiple storage buckets by passing a unique name in the GetStorage instance as follows:

Press + to interact
await GetStorage.init('users');
final GetStorage usersBox = GetStorage('users');

Write data

Once we have the bucket in place, the next step is to write into it. We can insert key-value pairs into the storage using the write method.

Press + to interact
box.write('key', 'value');

We can conditionally ...