【发布时间】:2022-06-19 17:32:34
【问题描述】:
我有这样的代码。但我在代码中收到_CastError (Null check operator used on a null value) 错误。
const secureStorage = FlutterSecureStorage();
final encryprionKey = secureStorage.read(key: 'key');
if (encryprionKey == null) {
final key = Hive.generateSecureKey();
await secureStorage.write(
key: 'key',
value: base64UrlEncode(key),
);
}
final key = await secureStorage.read(key: 'key');
final encryptionKey = base64Url.decode(key!);
print('Encryption key: $encryptionKey');
final encryptedBox= await Hive.openBox('vaultBox', encryptionCipher:
HiveAesCipher(encryptionKey));
encryptedBox.put('secret', 'Hive is cool');
print(encryptedBox.get('secret'));
我该如何解决?
【问题讨论】:
-
什么是堆栈跟踪,什么是违规行?当你调用
base64Url.decode(key!)时,你确定key不能是null吗? -
@jamesdlin 密钥已经生成。也就是说,数据是预先保存的。
-
可能
key没有生成/保存,尝试打印看看是不是== null -
在
if (encryprionKey == null) {这一行,警告是什么? -
@Belinda G. Freitas
The operand can't be null, so the condition is always false. Try removing the condition, an enclosing condition, or the whole conditional statement.