data_save 0.4.4
data_save: ^0.4.4 copied to clipboard
datasave for flutter and flutter web
data_save #
One API for storing small values in Flutter applications. data_save uses
SharedPreferences on native platforms and persistent cookies on Flutter Web.
Features #
- Supports
String,bool,int, anddoublevalues. - Uses URI encoding for safe Unicode and delimiter storage on the web.
- Uses host-only cookies with
SameSite=LaxandSecureon HTTPS. - Supports Flutter Web builds compiled to JavaScript or WebAssembly.
Installation #
dependencies:
data_save: ^0.4.0
Usage #
Initialize the library before reading or writing values:
import 'package:data_save/DataSave.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await DataSave.init();
runApp(const MyApp());
}
Store and retrieve values:
await DataSave.setString('name', 'Sara');
await DataSave.setBool('darkMode', true);
await DataSave.setInt('launchCount', 4);
await DataSave.setDouble('volume', 0.75);
final name = DataSave.getString('name');
final darkMode = DataSave.getBool('darkMode');
final launchCount = DataSave.getInt('launchCount');
final volume = DataSave.getDouble('volume');
Remove stored values:
await DataSave.removeAll();
Reset a corrupt Windows preferences file #
Windows automatically detects the shared_preferences.json file opened by
SharedPreferences. No path or additional dependency is needed:
await DataSave.init();
On Windows, if loading throws FormatException, the detected file is deleted and the
error is rethrown. Close and reopen the app: preferences will be empty and the
first successful save recreates the file. All values in the deleted file are
lost.
Other errors (including access and disk errors) do not trigger deletion.
Other platforms retain their previous behavior. The optional
windowsPreferencesFilePath argument is still available to override detection;
if used, supply only the full path of this application's preferences file.
If the backend does not expose a unique preferences file, automatic deletion is
skipped and the load error is rethrown.
This recovers from an unreadable preferences document; it does not prevent
power-loss corruption or detect damaged JSON stored inside a string value.
Larger strings #
Use the large-string API for JSON and other values that can exceed the cookie
limit. It uses localStorage on web and the same SharedPreferences storage
as the normal API on native platforms:
await DataSave.setLargeString('economicCalendar', jsonString);
final jsonString = DataSave.getLargeString('economicCalendar');
await DataSave.remove('economicCalendar');
await DataSave.removeAllLarge();
On web, removeAll() clears both cookies and the origin's entire
localStorage. On native platforms, normal and large-string methods use the
same SharedPreferences storage.
Web behavior #
Web values are stored as first-party cookies for 90 days. Browser cookie limits apply, so this package is intended for small values rather than documents or large JSON payloads.
Cookies created in browser code cannot use the HttpOnly attribute. Do not use
this package to store passwords, private keys, or long-lived authentication
secrets.