简体   繁体   中英

GetStorage always returns null in flutter

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?

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")}");
}

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

main() async {

  await GetStorage.init();


}

Create an instance from GetStorage, I always 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('XXX', 1);
var a = await getStorage.read('XXX');
print(a); /// 1

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

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 ...;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM