颤振:共享首选项单例未正确初始化



我对Flutter和Dart很陌生,来自android,带来了我的一些兔子,我想实现一个SharedPreferences单例对象来简化和避免重复(重复(。 这是我的共享首选项单例类:

import 'package:shared_preferences/shared_preferences.dart';
import 'package:synchronized/synchronized.dart';
class MySharedPreferences {
static MySharedPreferences _instance;
SharedPreferences _preferences;
// keys
final String _logged = "LOGGED";
final String _accessToken = "ACCESS_TOKEN";
MySharedPreferences._() {
_initSharedPreferences();
}
static MySharedPreferences getInstance() {
var lock = new Lock();
if (_instance == null) {
lock.synchronized(() => {_instance = new MySharedPreferences._()});
return _instance;
} else
return _instance;
}
_initSharedPreferences() async {
_preferences = await SharedPreferences.getInstance();
}
bool checkLogged() {
return _preferences.getBool(_logged);
}
void setLogged(bool logged) {
_preferences.setBool(_logged, logged);
}

嗯,这个逻辑的大部分是我曾经在Android中做的事情,并且曾经完美地工作过,但是当我尝试测试它时,单例总是为空,这是测试:

import 'package:flutter_test/flutter_test.dart';
import 'package:reportingsystem/local/my_shared_preferences.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('Test the shared_preferences', () {
MySharedPreferences preferences = MySharedPreferences.getInstance();
preferences.setLogged(true);
expect(preferences.checkLogged(), true);
preferences.setLogged(false);
expect(preferences.checkLogged(), false);
});
}

测试失败是因为"首选项"对象为空,我不知道有什么问题,而且我在文档中没有找到太多关于它的信息。 下面是堆栈跟踪:

dart:core                                                       Object.noSuchMethod
package:reportingsystem/local/my_shared_preferences.dart 34:18  MySharedPreferences.setLogged
testshared_preferences_test.dart 8:17                          main.<fn>
testshared_preferences_test.dart 6:39                          main.<fn>
NoSuchMethodError: The method 'setBool' was called on null.
Receiver: null
Tried calling: setBool("LOGGED", true)

下面是一个示例,在该示例中,您必须在首次调用单一实例时调用init,然后才能同步访问它。

class MySharedPreferences {
static final MySharedPreferences _instance = MySharedPreferences._internal();
MockSharedPreferences prefereces;
factory MySharedPreferences() {
return _instance;
}
Future<void> init() async {
if (prefereces != null) {
return;
}
prefereces = await Future.delayed(Duration(seconds: 1), () => MockSharedPreferences());
}
MySharedPreferences._internal();
}
class MockSharedPreferences {
final Map<String, bool> data = {};
void setBool(String key, bool value) {
data[key] = value;
print('data $data');
}
}

然后你可以在第一次初始化后使用它而无需await,如下所示:

Future<void> main() async {
await first();
anyOther();
}
void anyOther() {
MySharedPreferences singleton = MySharedPreferences();
singleton.prefereces.setBool('first', true);
}
Future<void> first() async {
MySharedPreferences singleton = MySharedPreferences();
await singleton.init();
singleton.prefereces.setBool('notFirst', true);
}

最新更新