【问题标题】:Hive encrypted box key storage in Flutter secure_storage null safety issueFlutter secure_storage null 安全问题中的 Hive 加密盒密钥存储
【发布时间】:2021-07-16 02:07:47
【问题描述】:

在这个问题上需要一些帮助,我已经尝试了所有方法,但真的不知道如何解决这个问题。

我正在根据 Hive documentation 应用代码,以使用secure_storage 进行加密存储和密钥存储。我选择使用具有空安全性的 dart 2.12 但这似乎是不可能的。我真的不知道真正的问题在哪里。

final FlutterSecureStorage secureStorage = const FlutterSecureStorage();
    var containsEncryptionKey = await secureStorage.containsKey(key: 'key');
    if (!containsEncryptionKey) {
      var key = Hive.generateSecureKey();
      await secureStorage.write(key: 'key', value: base64UrlEncode(key));
    }
    var encryptionKey = base64Url.decode(await secureStorage.read(key: 'key'));
    var encryptedBox =
        await Hive.openBox<Sale>(kSalesBox, encryptionCipher: HiveAesCipher(encryptionKey));

上面的问题突出显示为:await secureStorage.read(key: 'key')

我已经尝试了所有方法,但我无法理解 IDE 想要什么,该消息在此应用程序中没有意义。

错误信息:

Decrypts and returns the value for the given [key] or null if [key] is not in the storage.

[key] shouldn't be null. [iOptions] optional iOS options [aOptions] optional Android options [lOptions] optional Linux options Can throw a [PlatformException].

The argument type 'String?' can't be assigned to the parameter type 'String'.

但密钥不会为空!这就是“if”检查的用途......

【问题讨论】:

    标签: flutter dart-null-safety


    【解决方案1】:

    通过随机猜测解决...

    将括号应用于错误突出显示和空断言运算符“!”如下:

    var encryptionKey = base64Url.decode((await secureStorage.read(key: 'key'))!);

    【讨论】:

    • ! 被称为 null-assertion-operator,它不会强制进行空值检查,而是告诉编译器你 100% 确定,结果永远不会为空。
    猜你喜欢
    • 2014-10-18
    • 2015-07-04
    • 1970-01-01
    • 2011-10-14
    • 2020-01-28
    • 2015-11-24
    • 2014-06-24
    • 2018-03-16
    相关资源
    最近更新 更多