my flutter application needs to check if there is a SharedPreferences
file and if there value in SharedPreferences
at the start of my application.
here is my code :
finally it generates the following error
my flutter application needs to check if there is a SharedPreferences
file and if there value in SharedPreferences
at the start of my application.
here is my code :
finally it generates the following error
Like it's mentioned in the comments, having an image with code is quite annoying, as you cannot copy to try some of it for yourself. Try to get the actual code in your question :)
Normally it should work already, by generating the file if it's not present.
// the below code is in an async function
final SharedPreferences prefs = await SharedPreferences.getInstance();
// the value of name can be null, so it's of type String?
final String? name = prefs.getString('name');
// the value of job cannot be null, as a default is given
// ?? ensures that if the left side is null, the default value 'jobless' is assigned
final String job = prefs.getString('job') ?? 'jobless';
Adapting this code should work for your case