Store key-value data on disk. Data may be persisted to disk asynchronously, and there is no guarantee that writes will be persisted to disk after returning, so this plugin must not be used for storing critical data.
Although key-value storage is easy and convenient to use, it has limitations:
Note: – Only primitive types can be used: – int, double, bool, string, and stringList. And it’s not designed to store a lot of data.
Following location Flutter use while storing local data on different platform.
Platform | Location |
Android | SharedPreferences |
iOS | NSUserDefaults |
Linux | In the XDG_DATA_HOME directory |
macOS | NSUserDefaults |
Web | LocalStorage |
Windows | In the roaming AppData directory |
- Get Instance and Save data
// obtain shared preferences
final prefs = await SharedPreferences.getInstance();
// set value
await prefs.setInt('counter', counter);
- Read data
final prefs = await SharedPreferences.getInstance();
// Try reading data from the counter key. If it doesn't exist, return 0.
final counter = prefs.getInt('counter') ?? 0;
- Remove data
final prefs = await SharedPreferences.getInstance();
await prefs.remove('counter');