Flutter SharedReferences.getInstance()始终返回null
来自面向对象编程的背景,我计划创建一个专用的设置类来存储应用程序的某些基本数据 我计划从使用SharedReferences和LocalStorage保存应用程序的主题开始 但是,SharedReferences.getInstance()似乎总是返回null 我尝试过简单地运行,在调试模式下运行,使用单独的异步方法加载SharedReferences并返回一个使用.then()展开的未来。我似乎不明白为什么在我编写的AppSettings.getInstance()方法中,SharedReferences.getInstance()总是为nullFlutter SharedReferences.getInstance()始终返回null,flutter,dart,Flutter,Dart,来自面向对象编程的背景,我计划创建一个专用的设置类来存储应用程序的某些基本数据 我计划从使用SharedReferences和LocalStorage保存应用程序的主题开始 但是,SharedReferences.getInstance()似乎总是返回null 我尝试过简单地运行,在调试模式下运行,使用单独的异步方法加载SharedReferences并返回一个使用.then()展开的未来。我似乎不明白为什么在我编写的AppSettings.getInstance()方法中,SharedRefe
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());
}
}
getInstance()应返回SharedReferences单实例。它一直返回null。您的函数是异步的,并且在返回
getInstance()
之后执行回调(,然后
)。您必须将函数更改为使用wait
并获取SharedReferences.getInstance()
的值,而不是使用SharedReferences.getInstance()。然后(…)
查看文档:
SharedReferences.getInstance()的实现
static Future getInstance()异步{
if(_instance==null){
最终地图优先权地图=
等待_getSharedReferencesMap();
_实例=SharedReferences.\uu0(首选项映射);
}
返回_实例;
}
您的函数是异步的,并且在返回getInstance()
后执行回调(然后
)。您必须将函数更改为使用wait
并获取SharedReferences.getInstance()
的值,而不是使用SharedReferences.getInstance()。然后(…)
查看文档:
SharedReferences.getInstance()的实现
static Future getInstance()异步{
if(_instance==null){
最终地图优先权地图=
等待_getSharedReferencesMap();
_实例=SharedReferences.\uu0(首选项映射);
}
返回_实例;
}
以下是根据奥古斯托的答案编写的代码:
static Future<AppSettings> getInstance() async {
_prefs = await SharedPreferences.getInstance();
_appSettings ??= AppSettings._();
return _appSettings;
}
static Future getInstance()异步{
_prefs=等待SharedReferences.getInstance();
_appSettings???=appSettings.uz();
返回appSettings;
}
以下是根据奥古斯托的答案编写的代码:
static Future<AppSettings> getInstance() async {
_prefs = await SharedPreferences.getInstance();
_appSettings ??= AppSettings._();
return _appSettings;
}
static Future getInstance()异步{
_prefs=等待SharedReferences.getInstance();
_appSettings???=appSettings.uz();
返回appSettings;
}
谢谢!让我试试看,我会回来报到的!非常感谢。让我试试看,我会回来报到的!