当前位置:   article > 正文

Flutter 本地存储工具类_flutter storage

flutter storage
import 'package:shared_preferences/shared_preferences.dart';

/// 数据存取工具类
/// 初始化:在main类中初始化: StorageUtils.getInstance();
/// 调用:StorageUtils.getString(ConstantUtils.userName);
class StorageUtils {
  static late StorageUtils _instance;
  static late SharedPreferences _sharedPreferences;

  static Future<StorageUtils> getInstance() async {
    _instance = await StorageUtils._init().getSharedPreferences();
    return _instance;
  }

  StorageUtils._init();

  Future getSharedPreferences() async {
    _sharedPreferences = await SharedPreferences.getInstance();
  }

  static saveInt(String key, int value) {
    _sharedPreferences.setInt(key, value);
  }

  static int getInt(String key, {int defaultVal = 0}) {
    return _sharedPreferences.getInt(key) ?? defaultVal;
  }

  static saveString(String key, String value) {
    _sharedPreferences.setString(key, value);
  }

  static String getString(String key, {String defaultVal = 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/308713
推荐阅读
相关标签
  

闽ICP备14008679号