Hive: deleteAt is making the value null instead of deleting it

Created on 15 Jul 2020  路  4Comments  路  Source: hivedb/hive

Question
So let's say I have a list of books. Deleting book at nth index using deleteAtis not deleting it and shifting (n+1)th element to its place, rather it is making it null. i.e. the element at nth is now null.

How to perform deleteAtperfectly?

BTW I have used delete outside of the ValueListenableBuilder.

Code sample

ValueListenableBuilder(
                    valueListenable: Hive.box('books').listenable(),
                    builder: (context, box, _) {
                      if (box.values.length == 0)
                        return Center(
                          child: Text("No books"),
                        );
                      return ListView.builder(
                        primary: true,
                        padding: EdgeInsets.only(bottom: 95),
                        itemCount: box.values.length,
                        itemBuilder: (context, int index) {
                          Book book = box.get(index);
                          return Padding(
                            padding:
                                const EdgeInsets.only(bottom: kMasterPadding),
                            child: BookItem(
                              title: book.title,
                              author: book.authorName,
                            ),
                          );
                        },
                      );
                    },
                  ),

code used for deletion

() async {
      await Hive.box("books").deleteAt(Hive.box("books").length - 2);
//deleted at last 2nd because deleting at the end was working perfectly
           },

Version

  • Platform: Android, Windows
  • Flutter version: [e.g. 1.17.5]
question

All 4 comments

I tried to reproduce the issue, but it works correctly.

CodeResult
import 'package:hive/hive.dart';

void main() async {
  var box = await Hive.openBox('writeNullBox');
  await box.clear();

  // Add some values
  for (int a=0;a<10;a++) box.add(a.toString());

  // All values
  print(box.valuesBetween());

  // Delete #3
  box.deleteAt(3);

  // All values
  print(box.valuesBetween());
}
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
(0, 1, 2, 4, 5, 6, 7, 8, 9)

Exactly. It is working but just not in my project. I can not find what's wrong.

@Illusion47586 could you try Book book = box.getAt(index); instead of box.get? It should work now.

And that just worked! Thank you so much.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aminjoharinia picture aminjoharinia  路  3Comments

azilvl picture azilvl  路  3Comments

SergeShkurko picture SergeShkurko  路  4Comments

jamesdixon picture jamesdixon  路  3Comments

cachapa picture cachapa  路  4Comments