我们如何将模型数据存储到 flutter 安全存储中……或者它支持吗?
我有一个这样的模型...我将数据从我的 API 加载到这个模型...一旦我有了数据,我想将其保存到 flutter 安全存储中,反之亦然(将整个数据从 flutter 安全存储加载到模型中) ...
class MyUserModel {
MyUserModel({
this.authKey,
this.city,
this.contact,
this.email,
this.isContact,
this.userId,
});
String authKey;
String city;
String contact;
dynamic email;
bool isContact;
int userId;
}
Run Code Online (Sandbox Code Playgroud)
当然,我知道我们可以像下面这样读取和写入数据...我只是检查是否有一种方法可以直接从模型写入数据...
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
// Create storage
final storage = new FlutterSecureStorage();
// Read value
String value = await storage.read(key: key);
// Write value
await storage.write(key: key, value: value);
Run Code Online (Sandbox Code Playgroud)
我看到 hive 开箱即用地支持此功能,但我意识到初始化也只需要很少的时间(2-3 秒)作者正在研究 hive 的替代方案,因为有两个主要块......名为Isar的新数据库可以清除所有障碍,但它仍在开发中......
如果可能的话请分享示例代码...
我正在使用 flutter secure storage (^5.0.2) 来保存令牌,但是当在模拟器上调用 storage.write() 时(我使用的是 Pixel 5 API 30),而当我使用它时,我收到以下错误在我的物理设备(Honor 8x)上运行良好:
PlatformException (PlatformException(Exception encountered, write, java.lang.NullPointerException: Attempt to invoke interface method 'byte[] com.it_nomads.fluttersecurestorage.ciphers.StorageCipher.encrypt(byte[])' on a null object reference
at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin.write(FlutterSecureStoragePlugin.java:202)
at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin.access$300(FlutterSecureStoragePlugin.java:37)
at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin$MethodRunner.run(FlutterSecureStoragePlugin.java:289)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.os.HandlerThread.run(HandlerThread.java:67)
, null))
Run Code Online (Sandbox Code Playgroud)
这是调试的输出:
W/KeyStore( 2975): KeyStore exception
W/KeyStore( 2975): android.os.ServiceSpecificException: (code 7)
W/KeyStore( 2975): at android.os.Parcel.createExceptionOrNull(Parcel.java:2387)
W/KeyStore( 2975): at android.os.Parcel.createException(Parcel.java:2357)
W/KeyStore( 2975): at android.os.Parcel.readException(Parcel.java:2340)
W/KeyStore( 2975): at android.os.Parcel.readException(Parcel.java:2282)
W/KeyStore( 2975): at android.security.keystore.IKeystoreService$Stub$Proxy.get(IKeystoreService.java:988)
W/KeyStore( 2975): at android.security.KeyStore.get(KeyStore.java:233) …Run Code Online (Sandbox Code Playgroud)