Zer*_*ine 3 dart flutter flutter-hive
我将列表保存到 Hive Box 中的索引中。
class Person {
String name;
Person(this.name);
}
List<Person> friends = [];
friends.add(Person('Jerry'));
var accountBox = Hive.openBox('account');
accountBox.put('friends',friends);
//Testing as soon as saved to make sure it's storing correctly.
List<Person> friends = accountBox.get('friends');
assert(friends.length == 1);
Run Code Online (Sandbox Code Playgroud)
所以这一切都按预期进行。由于某些疯狂的原因,当我热重启应用程序并尝试从 Hive 获取好友列表时,它不再返回List<Person>
. 它返回一个List<dynamic>
var accountBox = Hive.openBox('account');
List<Person> friends = accountBox.get('friends');
///ERROR
E/flutter (31497): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled
Exception: type 'List<dynamic>' is not a subtype of type 'List<Person>'
E/flutter (31497): <asynchronous suspension>
etc...
Run Code Online (Sandbox Code Playgroud)
可能是什么原因造成的?这太不寻常了。
小智 8
这为我解决了问题
var fooBox = await Hive.openBox<List>("Foo");
var foosList = fooBox.get("foos", defaultValue: []).cast<Foo>();
print(foosList);
Run Code Online (Sandbox Code Playgroud)
这个解决方案来自github问题
归档时间: |
|
查看次数: |
4982 次 |
最近记录: |