Reputation: 531
How check string value from sharedPreferences list is null or empty? If null or empty go to Login page else print value.
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
final myStringList = sharedPreferences.getStringList('my_sharedPreferences_list') ?? [];
if ( **myStringList[0] is null or empty** ) {
print('> EMAIL not found!');
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => Login()
));
}
else { print(myStringList[0].toString()); }
Upvotes: 0
Views: 64
Reputation: 1770
You have two way to check null
The first likes Yeasin if(myStringList==null )
and the second set default value when you get like this
final myStringList =
sharedPreferences.getStringList('my_sharedPreferences_list') ?? '';
Then check with empty or not empty
if (myStringList.isEmpty) {}
if (myStringList.isNotEmpty) {}
Upvotes: 0
Reputation: 338
You can check if your list is null or empty using this command myStringList == null || myStringList.isEmpty
.
||
indicates or
operator
Upvotes: 0
Reputation: 63689
getStringList
returns nullable list of String
List<String>?
. You can simply check
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
final myStringList =
sharedPreferences.getStringList('my_sharedPreferences_list');
if(myStringList==null ){
print('> EMAIL not found!');
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => Login()
));
}
else { print(myStringList[0].toString()); }
Upvotes: 1