I'm Tryring to add products to cart without User login in my app. Then If user LOGIN it needs to show the same products which is user add his cart without Login. But I'm facing problem when passing a random integer value from SharedPreferences. I think its changing when the app restarts.
Am I doing somthing wrong?
For that I 1st generate random integer number.
Function call to generate random number and saved in SharedPreferences
funForCookie(Random().nextInt(90) 10);
funForCookie() function implementation:
void funForCookie(int RandomValue) async {
SharedPreferences pref = await SharedPreferences.getInstance();
await pref.setInt("randomNumValuekey", RandomValue);
}
Then storing it in variable RandomNumber
to pass it to my API as parameter:
int? RandomNumber;
void getCred() async { //======HERE WE FETCH OUR CREDENTIALS FROM SHARED PREF
SharedPreferences pref = await SharedPreferences.getInstance();
setState(() {
RandomNumber = pref.getInt("randomNumValuekey");
});
Add to cart Code snippet with api
CodePudding user response:
do you await
funForCookie(Random().nextInt(90) 10)
before you call getCred()
?
You are probably just missing the await
keyword