Search⌘ K

Storage Implementation

Explore how to efficiently manage local storage in Flutter using GetX's GetStorage. Learn to initialize storage, write and read key-value pairs, delete entries, and listen to changes. This lesson guides you through building a storage table and input interface to interact with the stored data, helping you implement persistent state management in your Flutter apps.

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.

Dart
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.

Dart
final GetStorage box = GetStorage();

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

Dart
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.

Dart
box.write('key', 'value');

We can conditionally write after ...