Here it is the post method of login API, When user will logged in the json object will automatically stored in shared Preferences I dont know how to store object in shared preferences
void login(String email, password) async {
try {
Response response = await post(Uri.parse('https://app.getjustdone.com/api/login'),
body: {
'email': email,
'password': password,
});
if (response.statusCode == 200) {
var data = jsonDecode(response.body.toString());
// print(data['user_info']);
pageRoute(data['token']);
setState(() {
isApiCallProcess = false;
});
showToast(context, data['message']);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomeScreen()),
);
} else {
var data = jsonDecode(response.body.toString());
showToast(context, data['message']);
}
} catch (e) {
setState(() {
isApiCallProcess = false;
});
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Server Error"),
actions: [
FlatButton(
child: Text("Try Again"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
}
void pageRoute(String token) async {
SharedPreferences pref = await SharedPreferences.getInstance();
await pref.setString("user", token);
}
Here is the json response I want to store in shared preferences
{
"status": true,
"message": "Log In Successful.",
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiMjIyMWI3MmU2N2U1YjBlZTgyMmY0YWI0OWNlM2ExZTg0OWI4ZDljMjMwYzZhN2YzOWQxNDVlZTQ2MTliOWM5MTY5ODQ5Mjk0YjYwNzUwMTAiLCJpYXQiOjE2NDk0ODQ1MzQuOTI3OTY3LCJuYmYiOjE2NDk0ODQ1MzQuOTI3OTcsImV4cCI6MTY4MTAyMDUzNC45MTkwNzQsInN1YiI6IjMiLCJzY29wZXMiOltdfQ.lc5Avuw28uVoOX6DMAin8Q09Gy-xbyg7GAOGWwjCIn_IpQxiM6KkLe9dzKxCVyyS8By9IJKbIYFpvFYAbN1XlHzZrGAKMkfAmtkMpll6p0RacvIQnPDLM3NhJzWSXRc5vbYKdeLQSjNqhCO08M2K83IVhRgYFL_6p77kRwndusltOXWQVzlIMGAITpNP2J32j2rISR1Zh9qrlDdEYcyt3jZcekD6BUZG_SxxH0PviApi4YwKEkmzEK2-JfW1qqppNWObL_Nfu4A4nIIN9XoAclwdKJ9CPAkT5-YY0YvjZea-eLkBpEiHb8SuxqwAHU4afxOif2zhAgjYrkq2C4MW67L8MfDuzB21TtM9iKygC5RCEuAuuPv5IGnJtouL287qmx_bKhgq-wu7rDGlHJm2obAChyMN81TRztrETRTpFeXG7vBu5raVk2zv2Ady5bST3Lzj3v_jqsZPyjWyc-2qMHYHc-3y18_psgytUZYtLvQAXvug6CvF9kOs96oUrcJAW7zit6lA4KzSsMa-LzcpMnB7ny2nAxKvIajpaPag-JCawN6Ll1A5rSeHCb3AD5Z-9vhV_AkYEicpiBLGuDf6MNDlfPLdZPDVoQ1d58GK8tR2vvZzJrFtIVafGRPq-SEbFus0N9h9fUtxXAAS36hMNO_NNbe1ucWLdIQnl5pizoE",
"user_info": {
"id": 3,
"name": "munna",
"role": "Master",
"email": "[email protected]",
"phone": "01903932723",
"image": "https://app.getjustdone.com/img/avatar.png",
"balance": "0.00"
}
}
CodePudding user response:
Shared preferences stores int, double, bool, String and List.
In this case you need to convert your json to string using json.encode(myjs)
If you want to access the object, you decode it using json.decode(mystring)
CodePudding user response:
SharedPreferences accepts strings as type of data to store. So you should just encode and decode the data.
How to save the JSON:
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("user", jsonEncode(token));
and how to get the JSON:
SharedPreferences prefs = await SharedPreferences.getInstance();
var myData = prefs.getString('user');
String token = jsonDecode(myData!); //Null check on the data you just got