0

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 :

main.dart

ShardPrefeferance to retrieve user data from sharedPreferanc

finally it generates the following error

generated error

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
nebi
  • 11
  • 4

1 Answers1

0

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

fravolt
  • 2,565
  • 1
  • 4
  • 19