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?
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
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);
How could you store something like that even when the app is closed in Flutter:
Class myList {
String id;
List<Item> list;
}
Class Item{
//Many property’s
}
I thought maybe I could do that with "sqflite", a flutter dependency, but I have no Idea how I could store there this List<Item>. Do you have any idea?
Btw: I need multiple of these "myList" instances.
You can store it using sqflite,hive or shared preferences.Try to save it under flutter shared preferences. These are two methods that you can use.
Using shared preferences
1)First create a shared preference instance
SharedPreferences prefs = await SharedPreferences.getInstance();
Then save the relevant data type.here you need to put a object list so add the list and convert it to the String using jsonEncode.
Map<String,List<Item>> map={mylist.id: mylist.list};
prefs.setString("itemList", json.encode(map));
Then you can retrieve data like this
Map<String,List<Item>> map=json.decode(prefs.getString("itemList")).asMap();
2)Using Hive
First Create the hive databse.you can put any name here
var box = Hive.box('myBox');
Then add the object in to that database
var myList = myList()
..id = your id here
..list = add list here;
var name = box.add(myList);
You can get anywhere this data list.
print(box.getAt(0));
You can try hive package. It is a lightweight local storage passage and it is a good idea to use hive because it has better benchmarks for reading and writes. You can search for the tutorial for the hive. or just read the documentation.
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.