gpt4 book ai didi

flutter - 读取 Hivebox 值返回 List 而不是保存的 List
转载 作者:行者123 更新时间:2023-12-03 04:24:29 25 4
gpt4 key购买 nike

我将列表保存到 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);

所以这一切都按预期工作。
由于某些疯狂的原因,当我热启动应用程序并尝试从 Hive 获取 friend 列表时,它不再返回 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...

这可能是什么原因造成的?这太不寻常了。

最佳答案

这为我解决了问题

var fooBox = await Hive.openBox<List>("Foo");

var foosList = fooBox.get("foos", defaultValue: []).cast<Foo>();
print(foosList);
此解决方案来自 github issue

关于flutter - 读取 Hivebox 值返回 List<dynamic> 而不是保存的 List<Object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60458669/

25 4 0