lesnitsky / flutter_localstorage

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

await storage.getItem('userObj') always return null value #81

Closed saravanans55 closed 3 years ago

saravanans55 commented 3 years ago

I'm using the below code in my flutter mobile app. It does not persist in storage value. When I close and reopen the mobile app it's automatically cleared.

My Code: Future isLoggedIn() async { bool flag = false;

await storage.ready;
print("storage.ready=======================>");
print(await storage.getItem('userObj'));

print("isLoggedIn---------------------------test");
print(await storage.getItem('testingLocal'));
if (await storage.getItem('userObj') != null && await storage.getItem('userObj') != "") {
  flag = true;
}

return flag;

}

lesnitsky commented 3 years ago

could you please also share a piece of code which sets the item to the localstorage?

saravanans55 commented 3 years ago

@lesnitsky for your reference,

` final LocalStorage storage = new LocalStorage('bskp_app');

Future userAuthAPI(username, password) async {

var inputObj = {'email': username, 'password': password, 'targetDomainKey': DOMAIN_KEY};
final url = "${API_BASE_PATH}/domain/login";
Map<String, String> headers = {'Content-Type': 'application/json'};

final response = await http.post(
    Uri.parse(url),
    headers: headers,
    body: jsonEncode(inputObj)
);
var resultObj = {};
var jsonData = json.decode(response.body);

if(response.statusCode == 200){
   resultObj = {
    "status" : true,
    "result" : jsonData,
  };
   storage.setItem('userObj', jsonData);

}else{
   resultObj = {
    "status" : false,
    "result" : jsonData,
  };
   storage.setItem('userObj', {});
}

return resultObj;

}`

lesnitsky commented 3 years ago

setItem returns Future so should be await-ed


if(response.statusCode == 200){
   resultObj = {
    "status" : true,
    "result" : jsonData,
  };
   await storage.setItem('userObj', jsonData);
   ^^^^ THIS IS IMPORTANT
}
saravanans55 commented 3 years ago

Thanks for the help @lesnitsky. I'll try and come back soon.

saravanans55 commented 3 years ago

@lesnitsky I tried But facing the same problem. This package working perfectly when I didn't close or clear the app from the recent apps list. If I close the apps it returns a null value.

FYR SetItem Result Screenshot

https://ibb.co/R3SVRCP

Reopen the app | getItem logs:-

2021-09-06 16:39:57.754 28880-28880/io..iotplatform D/SurfaceView: UPDATE null, mIsCastMode = false 2021-09-06 16:39:57.925 28880-28918/io..iotplatform I/flutter: storage.ready=======================> 2021-09-06 16:39:57.925 28880-28918/io..iotplatform I/flutter: null 2021-09-06 16:39:58.195 28880-28880/io..iotplatform I/FLTFireBGExecutor: Creating background FlutterEngine instance, with args: [] 2021-09-06 16:39:58.581 28880-28880/io..iotplatform I/FLTFireMsgService: FlutterFirebaseMessagingBackgroundService started! 2021-09-06 16:39:59.156 28880-28902/io..iotplatform I/kap.iotplatfor: ProcessProfilingInfo new_methods=0 is saved saved_to_disk=0 resolve_classes_delay=8000 2021-09-06 16:40:02.849 28880-28918/io..iotplatform I/flutter: isLoggedIn=======================>userObj 2021-09-06 16:40:02.849 28880-28918/io..iotplatform I/flutter: null 2021-09-06 16:40:03.156 28880-28918/io..iotplatform I/flutter: storage.ready=======================> 2021-09-06 16:40:03.156 28880-28918/io..iotplatform I/flutter: null

saravanans55 commented 3 years ago

Hi @lesnitsky I troubleshoot the problem. In this app, I used three different localstorage packages. One of the packages affecting this one. I tried your package with another sample project it's working perfectly. Thanks for your help ❤️.