参数类型 'List<Food>' 无法分配给参数类型 'Food' Hive db
The argument type 'List<Food>' can't be assigned to the parameter type 'Food' Hive db
我想创建一个页面,将所选项目放在最喜欢的页面上,所以我找到了 Hive 数据库,并在它的文档中找到了如何正确执行此操作。我尝试过这种方式,现在我被卡住了,因为没有选择最喜欢的项目并将其放在另一页上。我还尝试了另一种方法:我创建了函数 onFavoritePress()
并在 onPressed
函数中使用了 if-else 语句,代码如下所示:
trailing: IconButton(
icon: getIcon(index),
onPressed: () {
if (box.containsKey(food[index].ttId)) {
box.delete(food[index].ttId);
} else {box.put(food[index].ttId, food);}
}));
但它给出了错误:The argument type 'List<Food>' can't be assigned to the parameter type 'Food'
然后我将 Box <Food> box
更改为 Box<dynamic> box
并且这个错误消失了,但它仍然不起作用并且没有将任何项目放在 fav 的页面上。我很困惑我做错了什么。
整个代码是:
主要:
main() async {
await Hive.initFlutter();
await Hive.openBox<Food>(favoritesBox);
第一页:
class _FoodTState extends State<FoodT> {
Box<Food> favoriteFoodBox;
@override
void initState() {
super.initState();
favoriteFoodBox = Hive.box(favoritesBox);
}
body: ValueListenableBuilder(
valueListenable: favoriteFoodBox.listenable(),
builder: (context, Box<Food> box, child) {
return ListView.builder(
itemCount: food.length,
itemBuilder: (builder, index) {
return ListTile(
title: Text(food[index].ttTitle),
trailing: IconButton(
icon: Icon(Icons.favorite),
onPressed: () {
if (box.containsKey(food[index].ttId)) {
box.delete(food[index].ttId);
} else {box.put(food[index].ttId, food);}
}));
});
第2页:
body: ValueListenableBuilder(
valueListenable: favoriteFoodBox.listenable(),
builder: (context, Box<Food> box, child) {
return ListView.builder(
itemCount: food.length,
itemBuilder: (builder, index) {
return ListTile(
title: Text(food[index].ttTitle),
trailing: IconButton(
icon: Icon(
Icons.clear,
color: Colors.red,
),
onPressed: () {
favoriteFoodBox.delete(food[index].ttId);
},
),
);
您打开了单个 Food
项目的 Hive 框,但在 put 方法中,您正在存储整个列表 (List<Food>
) - 错误消息中提到了这一点。调整您的代码并更新您的 put
方法以存储单个 Food
项目:
trailing: IconButton(
icon: getIcon(index),
onPressed: () {
if (box.containsKey(food[index].ttId)) {
box.delete(food[index].ttId);
} else {
box.put(food[index].ttId, food[index]); // Changed put method
}
}));
之前,您存储了整个列表,但您只需要按 id 存储单个对象:food
-> food[index]
.
我想创建一个页面,将所选项目放在最喜欢的页面上,所以我找到了 Hive 数据库,并在它的文档中找到了如何正确执行此操作。我尝试过这种方式,现在我被卡住了,因为没有选择最喜欢的项目并将其放在另一页上。我还尝试了另一种方法:我创建了函数 onFavoritePress()
并在 onPressed
函数中使用了 if-else 语句,代码如下所示:
trailing: IconButton(
icon: getIcon(index),
onPressed: () {
if (box.containsKey(food[index].ttId)) {
box.delete(food[index].ttId);
} else {box.put(food[index].ttId, food);}
}));
但它给出了错误:The argument type 'List<Food>' can't be assigned to the parameter type 'Food'
然后我将 Box <Food> box
更改为 Box<dynamic> box
并且这个错误消失了,但它仍然不起作用并且没有将任何项目放在 fav 的页面上。我很困惑我做错了什么。
整个代码是: 主要:
main() async {
await Hive.initFlutter();
await Hive.openBox<Food>(favoritesBox);
第一页:
class _FoodTState extends State<FoodT> {
Box<Food> favoriteFoodBox;
@override
void initState() {
super.initState();
favoriteFoodBox = Hive.box(favoritesBox);
}
body: ValueListenableBuilder(
valueListenable: favoriteFoodBox.listenable(),
builder: (context, Box<Food> box, child) {
return ListView.builder(
itemCount: food.length,
itemBuilder: (builder, index) {
return ListTile(
title: Text(food[index].ttTitle),
trailing: IconButton(
icon: Icon(Icons.favorite),
onPressed: () {
if (box.containsKey(food[index].ttId)) {
box.delete(food[index].ttId);
} else {box.put(food[index].ttId, food);}
}));
});
第2页:
body: ValueListenableBuilder(
valueListenable: favoriteFoodBox.listenable(),
builder: (context, Box<Food> box, child) {
return ListView.builder(
itemCount: food.length,
itemBuilder: (builder, index) {
return ListTile(
title: Text(food[index].ttTitle),
trailing: IconButton(
icon: Icon(
Icons.clear,
color: Colors.red,
),
onPressed: () {
favoriteFoodBox.delete(food[index].ttId);
},
),
);
您打开了单个 Food
项目的 Hive 框,但在 put 方法中,您正在存储整个列表 (List<Food>
) - 错误消息中提到了这一点。调整您的代码并更新您的 put
方法以存储单个 Food
项目:
trailing: IconButton(
icon: getIcon(index),
onPressed: () {
if (box.containsKey(food[index].ttId)) {
box.delete(food[index].ttId);
} else {
box.put(food[index].ttId, food[index]); // Changed put method
}
}));
之前,您存储了整个列表,但您只需要按 id 存储单个对象:food
-> food[index]
.