Flutter SharedPreferences Windows location - flutter

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

Related

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?
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);

Is it possible to group SharedPreferences in flutter?

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 to use shared_preferences in provider on flutter....?

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.

save data when app closed in flutter provider

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.

How can I read and Write Files in Flutter Web?

I am working on flutter-web, I would like to do file operations(read and write) in flutter-web.
For Android and IOS, I was using path_provider dependency. But in Flutter-web, it is not supported.
Can anyone help me with this?
The accepted answer is not completely right. Yes, dart:io is not available on the web, but it is still possible to read files. You can select a file through the system's file picker and read it afterward.
An easy option to "write" a file is to send the user an auto-download.
Read:
Use this library to select a file: pub.dev/packages/file_picker (Web migration guide)
import 'dart:html' as webFile;
import 'package:file_picker_web/file_picker_web.dart' as webPicker;
if (kIsWeb) {
final webFile.File file = await webPicker.FilePicker.getFile(
allowedExtensions: ['pd'],
type: FileType.custom,
);
final reader = webFile.FileReader();
reader.readAsText(file);
await reader.onLoad.first;
String data = reader.result;
}
Write (a.k.a download):
import 'dart:html' as webFile;
if (kIsWeb) {
var blob = webFile.Blob(["data"], 'text/plain', 'native');
var anchorElement = webFile.AnchorElement(
href: webFile.Url.createObjectUrlFromBlob(blob).toString(),
)..setAttribute("download", "data.txt")..click();
}
in the dart docs it has been explained ... dart:io is not available to web build ... it means that you can not directly access to system files in flutter web . the build will fail .. you need to remove the code in order to run it
this is a security thing .. js doesn't allow it too .. imagine a website that can read all your system files if you visit it .. that's why things like path reading or file reading is not allowed .. it goes same for flutter
hope you have your answer
I created a package specifically for this before Flutter web was a thing. https://pub.dev/packages/async_resource
It looks at things as either a network resource (usually over HTTP), or a local resource such as a file, local storage, service worker cache, or shared preferences.
It isn't the easiest thing in the world but it works.
What about "dart:indexed_db library", You can store structured data, such as images, arrays, and maps using IndexedDB. "Window.localStorage" can only store strings.
dart:indexed_db
Window.localStorage
When it comes specifically to reading a file from disk on web, the easiest way is to use a PickedFile like so:
PickedFile localFile = PickedFile(filePath);
Uint8List bytes = await localFile.readAsBytes();