Reputation: 358
How to check SharedPreferences key if empty or null?
when i run the program it give me an error
1.] Future<String> getStrings() async {
2.] Future<SharedPreferences> _sPrefs = SharedPreferences.getInstance();
3.] final SharedPreferences prefs = await _sPrefs;
4.] appid = prefs.getString('appid');
5.] print(appid);
6.] if (appid != '') {
7.] return appid;
8.] }
9.] return ''; }
Above is my code, the error is only showing when i run my logout function that has prefs.clear()
;
Upvotes: 1
Views: 3099
Reputation: 51751
Change (appid != '')
to (appid != null)
. Null and an empty string are different things.
You can shorten this using a null-aware operator to:
return appid ?? '';
Upvotes: 3