3

Code

    print("Before : ${GetStorage().read("XXX")}");
    GetStorage().write("XXX", 1);
    print("After : ${GetStorage().read("XXX")}");

This is my Code. Every time I run the App, the Output is

Before : null
After : 1

Why is the storage data getting cleared everytime I restart the App? I thought this was an alternative to SharedPreference which works just fine. Have I missed something?

Prateek BB
  • 167
  • 1
  • 9
  • 1
    Where does the `GetStorage` method comes from? If it is `get_storage` do you call `await GetStorage.init();` somewhere before using the storage? – julemand101 Jul 05 '21 at 16:11

3 Answers3

8

Before anything, initialize the package, normally I do this on main.dart

main() async {

  await GetStorage.init();


}

Create an instance from GetStorage, I like to put a name on the box, if not it will put "GetStorage" by default. It needs to have a name so it can retrieve your data.

GetStorage getStorage = GetStorage('myData');

After that you can write and retrieve data from it, I recommend you to "await" all reads and writes.

await getStorage.write('test', 1);
var a = await getStorage.read('test');
print(a); /// 1

I recommend you to put a name on the box according to what you are storing.

djalmafreestyler
  • 1,703
  • 5
  • 21
  • 42
3

You should await for GetStorage.init().

void main() async {
  await GetStorage.init();
  print("Before : ${GetStorage().read("XXX")}");
  GetStorage().write("XXX", 1);
  print("After : ${GetStorage().read("XXX")}");
}
josxha
  • 1,110
  • 8
  • 23
0
final _userBox = () => GetStorage('User');

class UserPref {

    void call(){
        _userBox.call()..initStorage;
    }

    dynamic setValueInt(String key, int value) {
        return 0.val(key, getBox: _userBox).val = value;
    }

    String setValue(String key, String value) {
        return ''.val(key, getBox: _userBox).val = value;
    }

    dynamic getValueInt(String key) {
        return (-1).val(key,getBox: _userBox).val;
    }

    dynamic getValue(String key) {
        return ''.val(key,getBox: _userBox).val;
    }

    void setUser(User user) {
        ''.val('uname', getBox: _userBox).val = user.uname ?? '';
        (-1).val('gender', getBox: _userBox).val = user.gender ?? -1;
        ''.val('born', getBox: _userBox).val = user.born.toString();
        true.val('enabled', getBox: _userBox).val = user.enabled ?? true;
    }

    User getUser() {
        final String? uname = ''.val('uname',getBox: _userBox).val;
        final int? gender = (-1).val('gender',getBox: _userBox).val;
        final DateTime? born = ''.val('born',getBox: _userBox).val == '' ? null : DateTime.parse(''.val('born',getBox: _userBox).val);
        final bool? enabled = true.val('enabled',getBox: _userBox).val;

        return User(
            uname: uname,
            gender: gender,
            born: born,
            enabled: enabled,
        );
    }
}

///INIT:
@override
void initState() {
    //The init function must be written separately from the read/write function due to being asynchronous. 
    UserPref().call();
}

//OR

Future<void> main() async {
    //await GetStorage.init();
    UserPref().call();
}


///USAGE:

class MyStatefulWidget extends StatefulWidget {
    final Users prefUser = UserPref().getUser();
...
}

//OR

 @override
 Widget build(BuildContext context) {
    final Users prefUser = UserPref().getUser();
    return ...;
 }
Nuzhat Ansari
  • 581
  • 5
  • 6