Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: remove all saved shared preferences

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?

like image 540
Code Hunter Avatar asked Jan 23 '19 12:01

Code Hunter


People also ask

Where shared preferences are stored in flutter?

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.

What is shared preferences in flutter?

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.


3 Answers

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

like image 140
Andrey Turkovsky Avatar answered Oct 13 '22 03:10

Andrey Turkovsky


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');
like image 37
GaganSailor Avatar answered Oct 13 '22 05:10

GaganSailor


try

final pref = await SharedPreferences.getInstance();
await pref.clear();
like image 16
Sami Kanafani Avatar answered Oct 13 '22 05:10

Sami Kanafani