gpt4 book ai didi

flutter - SharedPreferences.getInstance() 总是返回 null

转载 作者:IT王子 更新时间:2023-10-29 07:11:09 25 4
gpt4 key购买 nike

来自面向对象的编程背景,我计划制作一个专门的设置类来存储有关应用程序的某些基本数据。

我计划从使用 SharedPreferences 和 LocalStorage 保存应用程序的主题开始。

但是,SharedPreferences.getInstance() 似乎总是返回 null。

我试过简单地运行,在 Debug模式下运行,使用单独的异步方法来加载 SharedPreferences 并返回一个使用 .then() 展开的 Future。我似乎无法弄清楚为什么我总是从我编写的 AppSettings.getInstance() 方法中的 SharedPreferences.getInstance() 得到 null。

import 'package:shared_preferences/shared_preferences.dart';
import 'package:localstorage/localstorage.dart';
import 'package:flutter/material.dart';

class AppSettings {
// Singleton Instance
static AppSettings _appSettings;

// For First Launch Recognition
bool _initialize;

// Storage instances for persistent settings storage
static SharedPreferences _prefs;
static LocalStorage _dayColors = new LocalStorage('_dayColors');
static LocalStorage _nightColors = new LocalStorage('_nightColors');

// App Settings
bool _nightTheme;

Color _dayBgColor;
Color _primaryDayColor;
Color _secondaryDayColor;
Color _accentDayColor;

Color _nightBgColor;
Color _primaryNightColor;
Color _secondaryNightColor;
Color _accentNightColor;

static AppSettings getInstance() {
SharedPreferences.getInstance().then((prefs) => _prefs = prefs);
_appSettings ??= AppSettings._();
return _appSettings;
}

///
/// Initialize App Settings
///
AppSettings._() {
_checkIfFirstLaunch();
if (_initialize) {
_loadDefaultSettings();
_saveSettings();
} else {
_loadSettings();
}
}

_checkIfFirstLaunch() {
try {
_initialize = _prefs.getBool("_initialize");
} catch (e) {
_initialize = true;
}
}

_loadSettings() {
_nightTheme = _prefs.getBool("_nightTheme");

_dayColors.ready.then((_) => _loadDayColors());
_nightColors.ready.then((_) => _loadNightColors());
}

_loadDefaultSettings() {
_nightTheme = false;

_dayBgColor = Colors.white;
_primaryDayColor = Colors.blue;
_secondaryDayColor = Colors.lightBlue;
_accentDayColor = Colors.blueAccent;

_nightBgColor = Colors.black54;
_primaryNightColor = Colors.green;
_secondaryNightColor = Colors.lightGreen;
_accentNightColor = Colors.amber;
}

_saveSettings() {
_prefs.setBool("_nightTheme", _nightTheme);

_dayColors.ready.then((_) => _saveDayColors());
_nightColors.ready.then((_) => _saveNightColors());
}
}

SharedPreferences.getInstance() 应该返回 SharedPreferences 单例实例。它一直返回 null。

最佳答案

您的函数是异步的,您的回调 (then) 在 getInstance() 返回后执行。您必须更改函数以使用 await 并获取 SharedPreferences.getInstance() 的值,而不是使用 SharedPreferences.getInstance().then(...)

查看文档:https://pub.dev/documentation/shared_preferences/latest/shared_preferences/SharedPreferences/getInstance.html

SharedPreferences.getInstance() 的实现。

static Future<SharedPreferences> getInstance() async {
if (_instance == null) {
final Map<String, Object> preferencesMap =
await _getSharedPreferencesMap();
_instance = SharedPreferences._(preferencesMap);
}
return _instance;
}

关于flutter - SharedPreferences.getInstance() 总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56761093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com