0

I have use shared preferences for store api token.but it not return full token. always missing some letters from end of token.

this is how a tried.

SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("bearer_token", response.data!.accessToken ?? "");


SharedPreferences prefs = await SharedPreferences.getInstance();
token = prefs.get('bearer_token').toString();
Karan Mehta
  • 1,442
  • 13
  • 32
Akitha
  • 15
  • 4

1 Answers1

2

To accept null value while receiveing you need to change

token = prefs.get('bearer_token').toString();

to

token = prefs.getString('bearer_token');// now it return nullable string

And it should define like String? token;.

Also you are saving empty string on null case.

You can do

prefs.setString("bearer_token", response.data?.accessToken ?? "");

Now check empty string like

final result = prefs.getString('bearer_token')??"";
if(result.isEmpty){
  ///....
}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • Thanks,But token is store correctly,issue is stored tokens last letters are missing.is there any length limitations like thing @Yeasin – Akitha Feb 09 '23 at 05:36
  • I've found [Shared Preferences - max length of a single value](https://stackoverflow.com/a/8517309/10157127) – Md. Yeasin Sheikh Feb 09 '23 at 05:45