Reputation: 253
I am using shared preferences package in flutter to store boolean values.
I have stored boolean value with key name.
But iam getting nosuchmenthodexception.
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
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
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
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