'Shared preferences null (getstring) error in flutter

I am working on flutter project and I save data to shared preferences for later use.While calling shared preferences as

tempData=sharedPreferences.getString('tempData');

it shows type error as

TypeError: Cannot read properties of null (reading 'getString')
    at success_page

This error is showing in all pages where I call shared preferences. How can I solve this issue?



Solution 1:[1]

the first thing I always do is make an instance of the sharedPreference class

 SharedPreferences prefs = await SharedPreferences.getInstance();

then I can use your methods

prefs.getBool(Constant().IS_TESTING);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elvis Salabarria Aquino