【问题标题】:How to delete data of shared-preferences from singleton in flutter?如何在颤动中从单例中删除共享首选项的数据?
【发布时间】:2020-12-04 15:31:42
【问题描述】:

我将一个文件创建为共享首选项,以便将我的应用程序中的数据存储在其中。它工作正常。但现在我想从存储中删除数据作为用户的按钮注销。因此,如果用户单击注销数据的按钮,将从共享首选项文件中清除。我如何从不同的班级做到这一点?

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;
  }


}

【问题讨论】:

  • 您可以继续进行的一种方法是将用户名和密码设置为空或一些空字符串,同时注销并在每次应用启动时检查用户名和密码是否为空

标签: flutter singleton static-methods


【解决方案1】:

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

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

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

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

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

在此之后,您将能够从任何类调用此方法,就像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();
     }
  }
}

【讨论】:

  • 啊,你可以使用给定的单例模式。由于您有 java 背景,您必须知道静态方法可以通过使用类名在类之外访问。静态方法只能访问类似于java的静态成员变量:)
  • 是的兄弟,我曾经在 java 中使用它作为将数据传递给其他类的意图
【解决方案2】:

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

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

【讨论】:

  • 嗨兄弟..我尝试这样,但它对我不起作用
猜你喜欢
  • 2013-03-23
  • 2020-05-30
  • 2020-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
相关资源
最近更新 更多