Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared_Preferences for Flutter Web?

Shared_preferences (https://pub.dev/packages/shared_preferences) doesn't seem to work for Flutter for Web.

I have the following function that's called when a button is pressed.

 getEmail() async {
    print("reached 1st line");
    SharedPreferences prefs = await SharedPreferences.getInstance();
    print("reached 2nd line");
    String _confirmedEmail = prefs.getString('_confirmedEmail') ?? "";
)

It prints "reached 1st line" but not "reached 2nd line", which means the program doesn't go past the await statement. Interestingly I don't get any error either. It seems to just ignore the rest of the function after the await statement.

What is the best alternative to store shared preferences in Flutter for Web?

like image 804
Jason O. Avatar asked Nov 03 '19 06:11

Jason O.


1 Answers

Great news, from version 0.5.6 shared_prefs flutter supports web by default

Now it's includes shared_preferences for web

Your code should work without changes, just update dependency in pubspec.yaml

dependencies:
 shared_preferences: ^0.5.6
like image 106
Vadim Popov Avatar answered Oct 14 '22 22:10

Vadim Popov