flutter - Flutter HiveDB 从数据库中删除
问题描述
在我们的文档中,Hive
我们有delete
从数据库中删除某些内容的方法,但是这种方法不会从数据库中删除,它只null
对找到的数据进行索引,当我们想要监听数据库更改或ListView
使用null
数据时,它会导致一些问题,
另一个问题是.values
返回non-nullable
数据,当我们尝试做一个ListView
我们得到null
错误
late Box<Sal> _sal;
useEffect((){
_sal = Hive.box<Sal>('sal') ;
});
// ...
ValueListenableBuilder(
valueListenable: _sal.listenable(),
builder: (_, Box<Sal> sal, __) => ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
padding: EdgeInsets.zero,
itemBuilder: (context, index) {
return Container(
height: 50.0,
margin: EdgeInsets.symmetric(vertical: 0.0),
child: Card(
color: DefaultColors.$lightBrown,
child: Row(
children: [
CText(
text: _sal.get(index)!.salName,
color: Colors.white,
style: AppTheme.of(context).thinCaption(),
).pOnly(right: 16.0),
const Spacer(),
IconButton(
icon: Icon(
Icons.edit,
color: Colors.yellow,
),
onPressed: () => showGeneralDialog(
//...
),
),
IconButton(
icon: Icon(
Icons.delete,
color: Colors.white,
),
onPressed: () => showGeneralDialog(
//...
),
),
],
),
),
);
},
itemCount: _sal.values.length,
),
).pSymmetric(
h: 16,
),
//...
}
解决方案
我找到了解决这个问题的方法
late Box<Sal> _sal;
late List<Sal> _data;
useEffect(() {
_sal = Hive.box<Sal>('sal');
_data = _sal.values.toList();
});
//...
ValueListenableBuilder(
valueListenable: Hive.box<Sal>('sal').listenable(),
builder: (_, Box<Sal> sal, __) {
_data = _sal.values.toList();
return ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
padding: EdgeInsets.zero,
itemBuilder: (context, index) {
return Container(
height: 50.0,
margin: EdgeInsets.symmetric(vertical: 0.0),
);
},
itemCount: _data.length,
);
},
),
//...
推荐阅读
- python - 无法构造反向牛顿多项式
- python - 缓存烧瓶登录 current_user.is_authenticated
- javascript - 在 PWA 中保持计时器运行的最佳实践
- tensorflow - 我当前在物理空间上保存的微调 BERT 模型需要 7GB 空间。模型占用这么大的空间是正常的吗?
- java - 是否可以用 20 位而不是 java 中的 8、16、32 位写入文件?
- python - Tensorflow LSTM 有状态选项不保持批次之间的状态
- java - 如何在 TextView 中显示 ArrayList 值?
- sql - 如何仅将日期时间的特定列转换为日期?
- mysql - 根据带有子选择的where语句更新mysql中的列
- reactjs - 使用反应返回 405 错误获取快速后端(来自原点'null'已被 CORS 策略阻止:对预检等的响应......)