如何从 flutter 中的单例中删除共享首选项的数据?

How to delete data of shared-preferences from singleton in flutter?

我为共享首选项制作了一个文件,以便在其中存储来自我的应用程序的数据。它的工作 fine.But 现在我想从存储中删除数据作为用户的按钮注销。因此,如果用户单击注销按钮,数据将从共享首选项文件中清除。我如何从不同的地方做到这一点 class?

import 'package:shared_preferences/shared_preferences.dart';

class MyPreferences{

  static const  USER = "user";
  static const  PASSWORD = "password";

  static final MyPreferences instance = MyPreferences._internal();


  //Campos a manejar
  SharedPreferences _sharedPreferences;
  String user = "";
  String password = "";

  MyPreferences._internal(){

  }

  factory MyPreferences()=>instance;

  Future<SharedPreferences> get preferences async{
    if(_sharedPreferences != null){
      return _sharedPreferences;
    }else{
      _sharedPreferences = await SharedPreferences.getInstance();
      user = _sharedPreferences.getString(USER);
      password = _sharedPreferences.getString(PASSWORD);
      return _sharedPreferences;

    }

  }
  Future<bool> commit() async {
    await _sharedPreferences.setString(USER, user);
    await _sharedPreferences.setString(PASSWORD, password);
  }

  Future<MyPreferences> init() async{
    _sharedPreferences = await preferences;
    return this;
  }


}

将您的共享首选项管理器 class 定义为给定的 singleton

class SharedPreferenceManager{
  static final SharedPreferenceManager _singleton = new SharedPreferenceManager._internal();

  factory SharedPreferenceManager() {
    return _singleton;
  }
  SharedPreferenceManager._internal() {
    ... // initialization logic here
  }
  ... // rest of the class
}

由此,您可以创建和访问该 class 的单个可重用实例。您可以在 class 中定义一个可以从外部访问的静态方法。由于静态方法只能访问静态数据成员,因此您应该将 sharedPrefernece 成员变量定义为静态。这是清除所有数据的方法。

static Future<bool> clearSharedPrefs(){
    SharedPreferences preferences = await SharedPreferences.getInstance();
    await preferences.clear();
}

在此之后,您将能够从任何 class 调用此方法,就像 SharedPreferenceManager.clearSharedPrefs().

对于数据库、网络和共享首选项相关任务,遵循单例模式是一种很好的做法。

这是您应该使用的代码。

import 'package:shared_preferences/shared_preferences.dart';

class MyPreferences{

  static const  USER = "user";
  static const  PASSWORD = "password";

  static final MyPreferences instance = MyPreferences._internal();

  static SharedPreferences _sharedPreferences;
  String user = "";
  String password = "";

  MyPreferences._internal(){}

  factory MyPreferences()=>instance;

  Future<SharedPreferences> get preferences async{
    if(_sharedPreferences != null){
      return _sharedPreferences;
    }else{
      _sharedPreferences = await SharedPreferences.getInstance();
      user = _sharedPreferences.getString(USER);
      password = _sharedPreferences.getString(PASSWORD);
      return _sharedPreferences;
    }
  }

  Future<bool> commit() async {
    await _sharedPreferences.setString(USER, user);
    await _sharedPreferences.setString(PASSWORD, password);
  }

  Future<MyPreferences> init() async{
    _sharedPreferences = await preferences;
    return this;
  }

  static Future<bool> clearPreference() async{
     if(_sharedPreferences){
         _sharedPreferences.clear();
     }
  }
}

您可以在共享首选项上使用 removeclear

SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.clear();
// OR
preferences.remove("MY KEY HERE");