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?
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);
Related
Where does SharedPreferences save local data on Windows? Like where is the path? I am referring to this:
final prefs = await SharedPreferences.getInstance();
var myData = prefs.getString('myData');
Thanks
According to the Shared Preferences documentation, it's stored in the local AppData folder for Windows.
From a scan of the code for the Windows platform interface, this seems to be the path obtained by pathProvider.getApplicationSupportPath(). You could call this in your code to get the specific path of where the file is stored.
It has moved apparently to \AppData\Local\Packages\YOURPACKAGE\LocalCache\Roaming\com.COMPANYNAME\APPNAME
In Native Android, we can specify a file name for the SharedPreferences
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
another one like
SharedPreferences pref2 = getSharedPreferences(SAMPLE_2, MODE_PRIVATE);
This way we can group values per file and possible to use same key for different shared files.
Is this possible with this shared_preferences flutter package or not?
I was working on a project for which I require the user to select a file from his/her device. The way I did it before flutter updated to enable null safety was that I would initialize an empty file and after the user picks the file the empty file gains the path of the selected file. This was the code to do it -
File myfile;
Future<void> _takePicture() async {
FilePickerResult? result =
await FilePicker.platform.pickFiles(type: FileType.image);
if (result != null) {
PlatformFile file = result.files.first;
setState(() {
myFile = File(file.path.toString());
});
But after I updated flutter to 2.5.2 it started giving an error , 'Non-nullable instance field 'myFile' must be initialized.'. My IDE suggests adding a late modifier before initializing the file, but after picking the file the app crashes due to that. Is there any way around this?
Please help
If your variable can be null, then just make it nullable:
File? myfile;
Null-Safety is a great feature to let you see faster and more effective when something can or cannot be null. But some variables can be null. That's totally fine. All null safety requires is that you declare them as such using the question mark as seen above.
I created an project using Provide. After, for saving changes on my app, I tried to use shared_preferences plugin. But I cant't use it...!
How can I do..?
You can write like this:
sharedPrefs = await SharedPreferences.getInstance();
sharedPrefs.setString('youVariableName', Provider.of<YourModel>(context, listen: false).youVariableName);
of course write this when you have value in Provider variable.
https://pub.dev/packages/provider
I used this package for the cart and it works.
But when the app closes, the cart model becomes empty.
What can I do for this problem?
I want to save the previous data when the app is opened.
thanks
you can also use the offline database
for example SQFLITE
Using something like Shared_Prefs sould solve it
it will store your data locally and retrieve them in your providers
create instance
SharedPreferences prefs = await SharedPreferences.getInstance();
set your data
await prefs.setInt('counter', 1);
in your provider retrieve if exist like this
if(prefs.getInt('counter') != null){
someVar = prefs.getInt('counter');
}
you can remove like
prefs.remove('counter');
Note: you can save/retrieve with one of the following types
Only primitive types can be used: int, double, bool, string, and stringList.