Home > Software design >  Shared Preferences misplugin
Shared Preferences misplugin

Time:04-07

i do same exactly as youtube says but in the end i got this error, do you know what is the problem ?

im using flutter 2.8.1 shared_preferences: ^2.0.13

this is the code

 class _AppHomeState extends State<AppHome> {
 final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
        Future loadData() async {
            final SharedPreferences prefs = await _prefs;
            var stringSet = await prefs.getString('sets');
            List setList = jsonDecode(stringSet!);
            for (var sets in setList) {
              c.setList.add(SetModel().fromJson(sets));
            }
          }
        
          Future saveData() async {
            final SharedPreferences prefs = await _prefs;
            List items = c.setList.map((e) => e.toJson()).toList();
            prefs.setString('sets', jsonEncode(items));
          }

enter image description here

CodePudding user response:

try running

flutter clean

see issue here

CodePudding user response:

You need to edit 31th row like this =>

final SharedPreferences _prefs = SharedPreferences.getInstance();

This fixes your problem.

  • Related