Hive: Does Hive have an equivalent to insertAt -- putAt overwrites current data in that position.

Created on 27 Nov 2020  路  1Comment  路  Source: hivedb/hive

Question
Hi I have a screen which uses the ValueListenableBuilder to show a list view of each item stored in a hiveDB. In that view I have each item wrapped in a 'Dismissible' for the user to delete. I am trying to implement an 'undo' feature incase they swipe to delete the wrong item. I would like the undo feature to replace the deleted item in the same position as it was originally deleted from. I am using box.putAt() but this is overwriting the record at has been moved into that position after the selected item was deleted. (i.e. If I delete the itemA at index 3, then itemB at index 4 moves to index 3, when I then use putAt index 3 the ItemB gets overwritten when replacing itemA.

I realise this is the intended behaviour of putAt so this isn't a bug, but I cannot see function which allows me to insert at a specific location without overwriting (like 'insert' in Python)

Code sample
return ListView.builder( scrollDirection: Axis.vertical, controller: _controller, reverse: true, shrinkWrap: true, itemCount: box.values.length, itemBuilder: (context, position) { var sampledata = box.getAt(position); return Dismissible( key: Key(position.toString()), onDismissed: (direction) { // Remove the item from the data source. setState(() { box.deleteAt(position); }); Get.snackbar( 'Record Deleted', 'Tap to undo', snackPosition: SnackPosition.BOTTOM, duration: Duration(seconds: 3), onTap: (_){ print(position); box.putAt(position, sampledata); } ); },

Version

  • Platform: iOS / Android
  • Flutter version: 1.22.4
  • Hive version: hive: ^1.4.4
question

Most helpful comment

Hey! Have you tried using the box method put(key, value)? The putAt method uses the indices of box.values that change everytime an item is inserted/deleted.

If you are not using HiveTypes, it should be fairly simple to override a key in the box, because most times we use strings as keys and if not, hive assigns an auto incremented key to the value you added.

Get the key of the value using box.keyAt(index) and use box.put(key, value) to insert at the exact location you had your value before.

If you are using HiveTypes, an easy way of working with auto increment is to have an int id field in your HiveObject and set it uppon insertion:

Future<void> insert(HiveObject myModel) async {
  final id = await box.add(myModel);
  myModel.id = id;
  myModel.save();
}

The box.deleteAt uses the indices of box.values
Delete the model using myModel.delete()

Save the id of the deleted model to a variable or add it to a list.

Then you can put it back into its key using box.put(id, myModel)

Just make sure you are not using the indices of box.values

>All comments

Hey! Have you tried using the box method put(key, value)? The putAt method uses the indices of box.values that change everytime an item is inserted/deleted.

If you are not using HiveTypes, it should be fairly simple to override a key in the box, because most times we use strings as keys and if not, hive assigns an auto incremented key to the value you added.

Get the key of the value using box.keyAt(index) and use box.put(key, value) to insert at the exact location you had your value before.

If you are using HiveTypes, an easy way of working with auto increment is to have an int id field in your HiveObject and set it uppon insertion:

Future<void> insert(HiveObject myModel) async {
  final id = await box.add(myModel);
  myModel.id = id;
  myModel.save();
}

The box.deleteAt uses the indices of box.values
Delete the model using myModel.delete()

Save the id of the deleted model to a variable or add it to a list.

Then you can put it back into its key using box.put(id, myModel)

Just make sure you are not using the indices of box.values

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aminjoharinia picture aminjoharinia  路  3Comments

MyoLinOo picture MyoLinOo  路  3Comments

yaymalaga picture yaymalaga  路  4Comments

Ferdzzzzzzzz picture Ferdzzzzzzzz  路  3Comments

ninest picture ninest  路  3Comments