To save and write data using GetX storage, follow the steps below:
- Import the necessary packages:
import 'package:get_storage/get_storage.dart';
import 'package:get/get.dart';
- Initialize the GetX storage:
void main() async {
await GetStorage.init();
runApp(MyApp());
}
- Write data to storage:
void saveData() {
final box = GetStorage();
box.write('name', 'John Doe');
box.writeIfNull('email', '[email protected]');
box.writeIfNotExist('age', 30);
}
- 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.