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.
Related
I'm getting The argument type BuildContext Function() can't be assigned to the parameter type BuildContext.
You can't send context to a function when it isn't even defined in the initstate function. Here you are trying to use provider outside the Build Function which is a no no. What you can do as an alternative is to wrap the function with a Future to give it a valid context :
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
myString = Provider.of<AppData>(context, listen: false).testString;
});
I solved this by removing the flutter whole folder where it is saved and than remove the Environmental Values and remove path and add new download new Flutter SDK and than again update the path in Environmental Values
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?
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.
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.