lesnitsky / flutter_localstorage

📦 LocalStorage for Flutter
MIT License
298 stars 65 forks source link

Nulls Return on Get Item #22

Closed imreallyliam closed 5 years ago

imreallyliam commented 5 years ago

A null returns the first time I use Get-Item on a certain page, this (due to the way code is written) causes data to be written as null.

What can be done to prevent nulls on the first call?

JanStorm commented 5 years ago

Its flutter. You can easily use if(data != null) to check if there exsists any data. In case you dont want the first time a null return, i would just add

Map<String, dynamic> data = storage.getItem('key');
if(data == null) {
    data = defaultData(); // Replace with own default data
}
...

Or did i misunderstand your issue?

imreallyliam commented 5 years ago

The issue was that it'd return null when there was data. If the data is null, the system overwrites it with default data, causing a reset.

ycgambo commented 5 years ago

If you got a null, it means you got nothing. I don't think anything is going wrong here. Also, It's your choice whether to overwrite or not. @xeu100

lesnitsky commented 5 years ago

@xeu100 make sure that you call getItem after await storage.ready (https://pub.dev/documentation/localstorage/latest/localstorage/LocalStorage/ready.html) there's also a chance something went wrong during data write, there's an error listener for this (https://pub.dev/documentation/localstorage/latest/localstorage/LocalStorage/onError.html)