I have initilized GetStorage() in main() and calling .read() in onReady dunction of GetX Controller but always get null!
Future<void> main() async {
await GetStorage.init();
runApp(const App());
}
class AuthenticationRepository extends GetxController {
static AuthenticationRepository get instance => Get.find();
/// Variables
GetStorage userStorage = GetStorage('User');
@override
void onReady() {
// Firebase User
firebaseUser = Rx<User?>(_auth.currentUser);
firebaseUser.bindStream(_auth.userChanges());
//Session
print('========= BEFORE -- ${userStorage.read('isFirstTime')} ===========');
userStorage.writeIfNull('isFirstTime', 'true');
print('========= AFTER -- ${userStorage.read('isFirstTime')} ============');
}
OUTPUT
================== BEFORE — null ========================
================== AFTER — true =========================
I have tried named values also like GetStorage(‘User’);
nothing worked.
2
Answers
Need to add
Change this to :
You will need to give the container name in init if you are using a custom container name.
So, you have two solutions
1 -> Update your init to this
OR
2 Don’t use a custom container name and GetStorage uses it’s default container name. In this case, update your code while declaring GetStorage object to read and write data
}