【发布时间】: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