使用 Hive 框数据构建小部件
Building widgets with Hive box data
我正在从我创建的 MyClass 文件中预定义的列表构建我的小部件。这行得通,但我希望能够存储持久数据以添加布尔收藏夹字段。
我为我的 class 创建了 Hive Types/Fields,生成了类型适配器,并在应用程序的第一个 运行 上成功加载了 Hive 框,我可以将值打印到控制台,所以我知道数据都在那里而且是正确的。
在 class 中,我有名称、图像 url 资产图像的路径和最喜欢的字段。
在我使用列表获取数据之前,我能够像这样获取图像 URL:
Expanded(child: Image.asset(widget.MyClass.imageURL)),
现在我想从 Hive 盒子中获取这个
Box<MyClass> box = Hive.box<MyClass>('myClassBox');
//This is where I am stuck
Expanded(child: Image.asset(box.???)),
我尝试了 box.values.where 和 box.get() 然后进入图像 URL 字段。但是获取需要一个密钥,我不必从
传递它
Widget build(BuildContext context)
然后我在尝试访问收藏字段时遇到了同样的问题,我正在使用收藏按钮包 (favorite_button 0.0.4)。然后我将根据被点击的按钮更新 true/false 值。
如果有人能为我指出正确的方向,那就太好了。
谢谢。
编辑:
这是小部件:
Widget build(BuildContext context) => GestureDetector(
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (context) => TaskPage(job: widget.job), //Need to get data from Hive now
)),
child: Container(
padding: const EdgeInsets.all(16),
height: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
),
child: Row(
children: [
Expanded(flex: 3, child: buildText()),
Expanded(child: Image.asset(widget.job.imageUrl)),//Need to get data from Hive now
GestureDetector(
child: Icon(
widget.job.fav ? Icons.favorite : Icons.favorite_border, //Need to get data from Hive now
),
onTap: () {
// add/remove from favorites list
}
),
],
),
),
);
第二次编辑:这是实施给出的建议后的相同代码
Widget build(BuildContext context) => GestureDetector(
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (context) => TaskPage(job: Hive.box<Job>('jobBox').get(context)), //This bit is still broken so I need to look at this
)),
child: Column(
children:
Hive.box<Job>('jobBox').values.toList().map(
(elementList) => Container(
padding: const EdgeInsets.all(16),
height: 100,
decoration: BoxDecoration(
color: white,
borderRadius: BorderRadius.circular(16),
),
child: Row(
children: [
Expanded(flex: 3, child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
elementList.name,
style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20),
),
SizedBox(height: 10),
//Text('Num tasks in job'),
],
)),
Expanded(child: Image.asset(elementList.imageURL)),
GestureDetector(
child: Icon(
elementList.fav
? Icons.favorite
: Icons.favorite_border,
color: elementList.fav ? Colors.red.shade200 : Colors.grey,
),
onTap: () {
//To do
}
// )
),
],
),
),
)
.toList(),
),
);
假设您的框中只有 1 个数据,您可以像这样访问存储的数据。
Box<MyClass> box = Hive.box<MyClass>('myClassBox');
if(box.isNotEmpty) {
final data = box.values.first;
// use data
} else {
// empty state
}
Hive 值可以有键,这取决于您如何使用它。如果您使用 box.put(key, value)
,则可以使用 box.get(key)
来处理键和值。
如果你使用box.add(value)
,它存储从0开始的自动分配索引的数据。所以你可以使用box.getAt(index)
获取带有索引的数据。
我正在从我创建的 MyClass 文件中预定义的列表构建我的小部件。这行得通,但我希望能够存储持久数据以添加布尔收藏夹字段。
我为我的 class 创建了 Hive Types/Fields,生成了类型适配器,并在应用程序的第一个 运行 上成功加载了 Hive 框,我可以将值打印到控制台,所以我知道数据都在那里而且是正确的。
在 class 中,我有名称、图像 url 资产图像的路径和最喜欢的字段。
在我使用列表获取数据之前,我能够像这样获取图像 URL:
Expanded(child: Image.asset(widget.MyClass.imageURL)),
现在我想从 Hive 盒子中获取这个
Box<MyClass> box = Hive.box<MyClass>('myClassBox');
//This is where I am stuck
Expanded(child: Image.asset(box.???)),
我尝试了 box.values.where 和 box.get() 然后进入图像 URL 字段。但是获取需要一个密钥,我不必从
传递它Widget build(BuildContext context)
然后我在尝试访问收藏字段时遇到了同样的问题,我正在使用收藏按钮包 (favorite_button 0.0.4)。然后我将根据被点击的按钮更新 true/false 值。
如果有人能为我指出正确的方向,那就太好了。
谢谢。
编辑:
这是小部件:
Widget build(BuildContext context) => GestureDetector(
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (context) => TaskPage(job: widget.job), //Need to get data from Hive now
)),
child: Container(
padding: const EdgeInsets.all(16),
height: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
),
child: Row(
children: [
Expanded(flex: 3, child: buildText()),
Expanded(child: Image.asset(widget.job.imageUrl)),//Need to get data from Hive now
GestureDetector(
child: Icon(
widget.job.fav ? Icons.favorite : Icons.favorite_border, //Need to get data from Hive now
),
onTap: () {
// add/remove from favorites list
}
),
],
),
),
);
第二次编辑:这是实施给出的建议后的相同代码
Widget build(BuildContext context) => GestureDetector(
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (context) => TaskPage(job: Hive.box<Job>('jobBox').get(context)), //This bit is still broken so I need to look at this
)),
child: Column(
children:
Hive.box<Job>('jobBox').values.toList().map(
(elementList) => Container(
padding: const EdgeInsets.all(16),
height: 100,
decoration: BoxDecoration(
color: white,
borderRadius: BorderRadius.circular(16),
),
child: Row(
children: [
Expanded(flex: 3, child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
elementList.name,
style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20),
),
SizedBox(height: 10),
//Text('Num tasks in job'),
],
)),
Expanded(child: Image.asset(elementList.imageURL)),
GestureDetector(
child: Icon(
elementList.fav
? Icons.favorite
: Icons.favorite_border,
color: elementList.fav ? Colors.red.shade200 : Colors.grey,
),
onTap: () {
//To do
}
// )
),
],
),
),
)
.toList(),
),
);
假设您的框中只有 1 个数据,您可以像这样访问存储的数据。
Box<MyClass> box = Hive.box<MyClass>('myClassBox');
if(box.isNotEmpty) {
final data = box.values.first;
// use data
} else {
// empty state
}
Hive 值可以有键,这取决于您如何使用它。如果您使用 box.put(key, value)
,则可以使用 box.get(key)
来处理键和值。
如果你使用box.add(value)
,它存储从0开始的自动分配索引的数据。所以你可以使用box.getAt(index)
获取带有索引的数据。