likes
comments
collection
share

【FlutterUtilCode】Flutter工具篇之LogUtils、SharedPerfsUtils

作者站长头像
站长
· 阅读数 23

前言

Flutter 从 2018年发展至今,生态可谓极其丰富,无论我们想要实现任何功能,基本都能找到对应的插件。但也正是因为其插件的丰富多样,导致我们经常需要花费大量的时间去挑选合适的插件,尤其是对于刚接触 Flutter 开发的新同学,有些甚至没有接触过客户端开发,面临的难度将会更大。

在 Android 开发的时候,我会经常使用 AndroidUtilCode 库来实现一些常用的函数和封装的系统API的调用来提高研发效率。但是很遗憾,在Flutter 中一直还没有出现类似的插件,本着 靠人不如靠己 的想法,自己把这两年项目中实现的工具类进行整理,于是就有了这个插件 FlutterUtilCode

目前 FlutterUtilCode 还在初期实现阶段,已经一些规划了一些待实现的工具类,大家如果有更多的建议,欢迎在 Github 中提 Issues

FlutterUtilCode 系列(一)—— Flutter工具篇之LogUtils、SharedPerfsUtils

FlutterUtilCode 系列(二)—— Flutter工具篇之ToastUtils

FlutterUtilCode 系列(三)—— Flutter工具篇之UuidUtils

本篇是【FlutterUtilCode】 系列文章的开篇,系列文章内容主要介绍插件中工具类的功能、用法和代码实现等,感兴趣的同学可以持续关注。

好了,废话不多说,我们开始今天的 《Flutter 工具篇之LogUtils、SharedPerenceUtils》 的介绍吧~

一、日志工具类-LogUtils

日志打印可谓是我们开发中必不可少的功能,在 Flutter 中我们一般通过debugPrint 来实现日志打印,这里的 LogUtils 也不例外,实现代码:

///  Name: Log工具类
///  Created by Fitem on 2023/5/31
class LogUtils {

  /// 是否开启日志,默认Debug模式下开启
  static bool isOpenLog = kDebugMode;

  /// 调试打印
  static void println(String obj) {
    if (isOpenLog) debugPrint(obj);
  }

  /// Log 用于网络请求等长内容日志
  static void logger(String obj, {StackTrace? stackTrace, int level = 0}){
    if (isOpenLog) log(obj, stackTrace: stackTrace, level: level);
  }
}

这里通过静态变量 isOpenLog 来实现日志打印的开关,默认是在 Debug模式 下开启,我们也可以手动修改。

除此之外,为了解决长日志打印不全的问题。这里提供了 logger() 方法调用,其内部通过 log() 方法实现。与 print() 函数或 debugPrint() 函数不同,log() 将日志写入环形缓冲区,并将其存储在内存中。虽然也会受到设备内存的限制,但是完全满足开发期间打印日志的需求。

使用案例:

// 打印日志
LogUtils.println('这是一条测试日志');

// 打印长日志
 LogUtils.logger('这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,'
        '这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,'
        '这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,'
        '这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,这是一条非常长的日志,');

// 控制打印开关
LogUtils.isOpenLog = value;

二、数据缓存工具类-SharedPrefsUtils

从事过客户端开发的同学都清楚,一般数据的缓存分为 SharedPreference数据库存储,其中 SharedPreference 主要是通过 Key-Value 键值对的形式存储数据。

这里我们基于 shared_preferences 插件来实现,该插件是Flutter官方发布的,目前 LIKES 7.4K、支持Flutter全平台、最近一次更新时间是 23 天前,可谓是异常受欢迎。

【FlutterUtilCode】Flutter工具篇之LogUtils、SharedPerfsUtils

SharedPrefsUtils 支持对 intDoubleBoolStringList<String> 类型数据的缓存和读取,数据读取支持添加默认值,并对每次操作添加打印日志便于我们查看数据,实现代码:

///  Name: SP工具类
///  Created by Fitem on 2023/5/31
class SharedPrefsUtils {

  /// 添加String类型数据
  /// [key] 键
  /// [value] 值
  static Future<void> putString(String key, String value) async {
    _println('putString', key, value);
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString(key, value);
  }

  /// 添加int类型数据
  /// [key] 键
  /// [value] 值
  static Future<void> putInt(String key, int value) async {
    _println('putInt', key, value);
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setInt(key, value);
  }

  /// 添加bool类型数据
  /// [key] 键
  /// [value] 值
  static Future<void> putBool(String key, bool value) async {
    _println('putBool', key, value);
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool(key, value);
  }

  /// 添加double类型数据
  /// [key] 键
  /// [value] 值
  static Future<void> putDouble(String key, double value) async {
    _println('putDouble', key, value);
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setDouble(key, value);
  }

  /// 添加List<String>类型数据
  /// [key] 键
  /// [value] 值
  static Future<void> putStringList(String key, List<String> value) async {
    _println('putStringList', key, value);
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setStringList(key, value);
  }

  /// 获取String类型数据
  /// [key] 键
  /// [defValue] 默认值
  static Future<String?> getString(String key, [String? defValue]) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? value = prefs.getString(key) ?? defValue;
    _println('getString', key, value);
    return value;
  }

  /// 获取int类型数据,如果没有则返回默认值
  /// [key] 键
  /// [defValue] 默认值
  static Future<int?> getInt(String key, [int? defValue]) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    int? value = prefs.getInt(key) ?? defValue;
    _println('getInt', key, value);
    return value;
  }

  /// 获取double类型数据,如果没有则返回默认值
  /// [key] 键
  /// [defValue] 默认值
  static Future<double?> getDouble(String key, [double? defValue]) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    double? value = prefs.getDouble(key) ?? defValue;
    _println('getDouble', key, value);
    return value;
  }

  /// 获取bool类型数据,如果没有则返回默认值
  /// [key] 键
  /// [defValue] 默认值
  static Future<bool?> getBool(String key, [bool? defValue]) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool? value = prefs.getBool(key) ?? defValue;
    _println('getBool', key, value);
    return value;
  }

  /// 获取List<String>类型数据,如果没有则返回默认值
  /// [key] 键
  /// [defValue] 默认值
  static Future<List<String>?> getStringList(String key, [List<String>? defValue]) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List<String>? value = prefs.getStringList(key) ?? defValue;
    _println('getStringList', key, value);
    return value;
  }

  /// 打印日志
  static void _println(String methodName, String key, dynamic value) {
    LogUtil.println('''SharedPref $methodName:
    key: $key 
    value: $value''');
  }
}

使用案例:

  /// 添加数据
  void _handlePut() {
    SharedPrefsUtils.putString(SPConstants.name, '小明');
    SharedPrefsUtils.putInt(SPConstants.age, 18);
    SharedPrefsUtils.putDouble(SPConstants.height, 175.5);
    SharedPrefsUtils.putBool(SPConstants.gender, true);
    SharedPrefsUtils.putStringList(SPConstants.family, ['爸爸', '妈妈', '哥哥', '弟弟']);
  }
  
  /// 获取数据
  Future<void> _handleGet() async {
    var name = await SharedPrefsUtils.getString(SPConstants.name, '');
    var age = await SharedPrefsUtils.getInt(SPConstants.age, 0);
    var height = await SharedPrefsUtils.getDouble(SPConstants.height, 0.0);
    var gender = await SharedPrefsUtils.getBool(SPConstants.gender, false);
    var family = await SharedPrefsUtils.getStringList(SPConstants.family, []);
  }

结语

好了,今天的工具类整理文章就到这里,目前插件已发布到 Pub 中,欢迎大家体验。

如果觉得这篇文章对你有所帮助的话,不要忘记一键三连哦,大家的点赞是我更新的动力🥰。

Pub: flutter_util_code

项目源码:FlutterUtilCode

使用案例:Example