【问题标题】:Flutter HiveDB deleting from databaseFlutter HiveDB 从数据库中删除
【发布时间】:2021-11-22 13:27:09
【问题描述】:

Hive 的文档中,我们有delete 用于从数据库中删除某些内容的方法,但此方法不会从数据库中删除,它只对找到的数据索引执行null,当我们想要时它会导致一些问题监听数据库更改或使用null 数据制作ListView

另一个问题是.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,
),

//...
}

【问题讨论】:

    标签: flutter dart hivedb


    【解决方案1】:

    我找到了解决这个问题的方法

    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,
      );
      },
    ),
    //...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多