Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is there way to get a particular data which have been saved using shared preference?

#i Saved all my user data using shared preference to perform autologin and it works well data is saved

 token = responseData['token'];
      userEmail = responseData['user_email'];
      userNicename = responseData['user_nicename'];
      userDisplayName = responseData['user_display_name'];
      userAddress = responseData['user_address'];
      userContact = responseData['user_contact'];
      userId = responseData['user_id'];
      userDisplayUrl = responseData['user_display_url'];
      notifyListeners();
      SharedPreferences prefs = await SharedPreferences.getInstance();
      final userData = jsonEncode({'token':token,'user_email':userEmail,'user_nicename':userNicename,'user_display_name':userDisplayName,'user_address':userAddress,'user_contact':userContact,'user_id':userId,'user_display_url':userDisplayUrl});
      prefs.setString('userData',userData);

#Data is saved in this manner

{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvc3dlZXQtYXJkaW5naGVsbGkuMy0xMDgtMTM4LTIwNi5wbGVzay5wYWdlIiwiaWF0IjoxNjQyMzMwNzQyLCJuYmYiOjE2NDIzMzA3NDIsImV4cCI6MTY0MjkzNTU0MiwiZGF0YSI6eyJ1c2VyIjp7ImlkIjoiMjgifX19.2jZEu-QNL3UxRiFSgVE728bF_cl_CZd0VJLT1f5HfCc","user_email":"sauravadhikari404@gmail.com","user_nicename":"sauravadhikari404","user_display_name":"SauravAdhikari404","user_address":null,"user_contact":null,"user_id":"28","user_display_url":""}

#now i wanna access single single data like i wanna get that user_id only or user_email only but i dont know how to do it i tried like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

String? userData;
  @override

  void initState(){
    // TODO: implement initState
    getUserId();
    super.initState();
  }
  void getUserId()async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      userData = prefs.getString("userData");
      print(userData);
    });
  }

#as i mentioned above all my data is comming in userData but now i wanna fetch my user_id only or user_email but i am unable to

>Solution :

Use jsonDecode to convert it to a Map:

Like so:

setState(() {
  var userId = jsonDecode(prefs.getString("userData"))["user_id"];
  print(userId);
});

To handle the null case:

String? userDataString = prefs.getString("userData");
if(userDataString != null){
  var userId = jsonDecode(userDataString)["user_id"];
  var email = jsonDecode(userDataString)["user_email"];
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading