I want to delete all saved shared preferences when the user taps on logout. Are there any ways to do this in a single shot without deleting one by one?
Flutter shared preferences is actually implemented as an in-memory cache. The first time that you call SharedPreferences. getInstance() all the current values are read from NSUserDefaults (on iOS) and SharedPreferences (on Android) and cached in memory.
shared_preferences is a Flutter plugin that allows you to save data in a key-value format so you can easily retrieve it later. Behind the scenes, it uses the aptly named SharedPreferences on Android and the similar UserDefaults on iOS.
I use shared_preferences
plugin:
In pubspec.yaml
dependencies:
flutter:
sdk: flutter
shared_preferences: ^0.4.3
And in dart file:
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
...
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.clear();
I think this is what you need
You can simply use clear()
function with your variable it will clear all the shared preferences.
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.clear();
If you want to remove particular key value from shared preferences with key name you can do it like this way as follows.
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.remove('KeyNameHere');
try
final pref = await SharedPreferences.getInstance();
await pref.clear();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With