flutterdartflutter-sharedpreference

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?


Solution

  • 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