Flutter HiveDB deleting from database

191 Views Asked by At

In documentation of Hive we have delete method for deleting something from database, but this method don't delete from database and it only do null on index of found data and it cause some problem when we want to listen to database changes or making ListView with null data,

another problem is .values that return non-nullable data and when we try to make a ListView we get null error

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,
),

//...
}
1

There are 1 best solutions below

0
On

I found solution for this problem

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