Samuel T
Samuel T

Reputation: 253

Getting null value when used to check values in Shared preferences

enter image description here

I am using shared preferences package in flutter to store boolean values. I have stored boolean value with key name. But iam getting nosuchmenthodexception. enter image description here

Edit: Code in text

 class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      SharedPreferences pref;
    
      Future<void> addStringToSF() async {
        pref = await SharedPreferences.getInstance();
      }
    
      @override
      void initState() async {
        super.initState();
        await addStringToSF();
      }
    
      @override
      Widget build(BuildContext context) {
        bool present = pref.containsKey('name');
    
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: present == true ? Home() : Register(),
        );
      }
    }

Upvotes: 0

Views: 666

Answers (3)

nvoigt
nvoigt

Reputation: 77334

You have not waited for your future to complete. In other words, you read from shared preferences, but basically said "I don't care if it's done yet, I'll just continue". Well, if you don't wait for it to be done, your value is not set yet.

Since it's a Stateful widget, you could assume your variable is false until your prefs variable is actually set. Once you are sure it's set, call setState, the build function will run again and will now have a valid value.

Alternatively, you could use a FutureBuilder to build your widget conditionally based on whether the future completed yet. You can find more information about that approach here: What is a Future and how do I use it?

Especially as a beginner or when coming from a different language (so basically everybody) you should install the pedantic package. It will give you a lot of warnings and hints what to do and not to do. It would have warned you of the fact that you missed to await a Future in line 26. Sure, experienced develpers see it, but it is so much easier to let a computer do it first. It's like compile errors. Sure you could find them on your own, but why would you not want your compiler to tell them which line they are on, right?

Upvotes: 3

Kaushik Bhingradiya
Kaushik Bhingradiya

Reputation: 907

You need to wait for getting SharedPreference instance because this is async method,You can get like below,

Define object above init,

SharedPreferences prefs;

Inside init method,

SharedPreferences.getInstance().then((SharedPreferences _prefs) {
      prefs = _prefs;

      setState(() {});
    });

Refer for more detail

Upvotes: 1

Ehsan Askari
Ehsan Askari

Reputation: 891

You need to wait for prefs to be initialized, then call containsKey() on it.
Ok, you have waited for prefs in adStringToSF but inside initState, you have not waited for adStringToSF, so build and adStringToSF executes concurrently.

Upvotes: 0

Related Questions