Home > Enterprise >  Flutter - How check string value from sharedPreferences
Flutter - How check string value from sharedPreferences

Time:11-10

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()); }

CodePudding user response:

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()); }

CodePudding user response:

You can check if your list is null or empty using this command myStringList == null || myStringList.isEmpty .

|| indicates or operator

  • Related