How save and write data using GetX storage?

18 min read

To save and write data using GetX storage, follow the steps below:

  1. Import the necessary packages:
import 'package:get_storage/get_storage.dart';
import 'package:get/get.dart';
  1. Initialize the GetX storage:
void main() async {
  await GetStorage.init();
  runApp(MyApp());
}
  1. Write data to storage:
void saveData() {
  final box = GetStorage();
  box.write('name', 'John Doe');
  box.writeIfNull('email', '[email protected]');
  box.writeIfNotExist('age', 30);
}
  1. Read data from storage:
void readData() {
  final box = GetStorage();
  final name = box.read('name');
  final email = box.read('email');
  final age = box.read('age');
  print('$name $email $age');
}

Note: To avoid data loss, make sure to call await GetStorage.init() before using GetX storage, as shown in step 2.