One API for storing small values in Flutter applications. data_save uses
SharedPreferences on native platforms and persistent cookies on Flutter Web.
- 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.
dependencies:
data_save: ^0.4.0Initialize 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();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 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.