first commit
This commit is contained in:
55
untitled/lib/utils/SharedPreferences.dart
Normal file
55
untitled/lib/utils/SharedPreferences.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/*
|
||||
* shared_preferences 配置
|
||||
*/
|
||||
class SPreferences {
|
||||
|
||||
static final SPreferences _instance = SPreferences._();
|
||||
factory SPreferences() => _instance;
|
||||
SPreferences._();
|
||||
static SharedPreferences _prefs;
|
||||
|
||||
|
||||
static Future<void> init() async {
|
||||
if (_prefs == null) {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
}
|
||||
}
|
||||
|
||||
// 设置数字类型
|
||||
Future<bool> setInt(String key, int value) async {
|
||||
return await _prefs.setInt(key, value);
|
||||
}
|
||||
|
||||
// 设置bool
|
||||
Future<bool> setBool(String key, bool value) async {
|
||||
return await _prefs.setBool(key, value);
|
||||
}
|
||||
|
||||
// 设置String
|
||||
Future<bool> setString(String key, String value) async {
|
||||
return await _prefs.setString(key, value);
|
||||
}
|
||||
|
||||
String getString(String key) {
|
||||
return _prefs.getString(key);
|
||||
}
|
||||
|
||||
bool getIsLogin() {
|
||||
if (_prefs.containsKey("isLogin")) {
|
||||
return _prefs.getBool("isLogin");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool getBool(String key) {
|
||||
if (!(_prefs.containsKey(key))) {
|
||||
_prefs.setBool(key, true);
|
||||
return false;
|
||||
}
|
||||
return _prefs.getBool(key);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user