HiCache.getInstance().get(HiConstants.theme); 获取到的结果是null

来源:12-7 系统Dark Mode变化监听与适配

夏目鲸鱼

2021-06-13

HiCache.getInstance().get(HiConstants.theme); 获取到的结果是null
这个我按照老师的代码敲的,打印出的结果是null

String theme = HiCache.getInstance().get(HiConstants.theme);


class HiConstants {
  static String authTokenK = "auth-Token";
  static String authTokenV = "MjAyMC0wNi0yMyAwMzoyNTowMQ==fa";
  static String courseFlagK = "course-flag";
  static String courseFlagV = "fa";
  static const theme = "System";
}

写回答

1回答

CrazyCodeBoy

2021-06-15

默认情况下因为还没有将theme存到本地所以从本地存储获取的theme是null,可参考下课程源码的实现加个默认值的处理:

///获取主题模式
ThemeMode getThemeMode() {
  String? theme = HiCache.getInstance().get(HiConstants.theme);
  switch (theme) {
    case 'Dark':
      _themeMode = ThemeMode.dark;
      break;
    case 'System':
      _themeMode = ThemeMode.system;
      break;
    default:
      _themeMode = ThemeMode.light;
      break;
  }
  return _themeMode!;
}


0
0

Flutter高级进阶实战-仿哔哩哔哩-掌握Flutter高阶技能

一次性掌握Flutter高阶技能+商业级复杂项目架构设计与开发方案

1722 学习 · 870 问题

查看课程