deleteRowObject method

Future<bool> deleteRowObject(
  1. List<int> rowIndices
)

Deletes multiple rows of an object in the current opened box

rowIndices : A list of indices of the rows to delete. Returns a Future

Implementation

Future<bool> deleteRowObject(List<int> rowIndices) async {
  final viewState = _hiveViewState;
  final currentBox = viewState.currentOpenedBox!;
  var boxValue = viewState.selectedBoxValue!;
  final nestedObjectIndices = viewState.objectNestedIndices!;
  if (nestedObjectIndices.isEmpty) {
    try {
      for (var index in rowIndices) {
        await currentBox.deleteAt(index);
      }
      return true;
    } catch (e) {
      _errorCallback(e.toString());
      return false;
    }
  } else {
    final firstIndexMap = nestedObjectIndices.first;
    final lastIndexMap = nestedObjectIndices.last;
    var refObj =
        boxValue[firstIndexMap.keys.single][firstIndexMap.values.single];
    if (nestedObjectIndices.length == 1) {
      for (int index in rowIndices) {
        refObj.removeAt(index);
      }
    } else {
      for (var indexMap in nestedObjectIndices..removeAt(0)) {
        if (indexMap == lastIndexMap) {
          for (int index in rowIndices) {
            refObj[indexMap.values.single].removeAt(index);
          }
        } else if (indexMap.keys.single == -1) {
          refObj = refObj[indexMap.values.single];
        } else {
          refObj = refObj[indexMap.keys.single][indexMap.values.single];
        }
      }
    }
    final replacementObject = boxValue[firstIndexMap.keys.single];

    return await _updateBoxWithObject(
      currentBox,
      firstIndexMap.keys.single,
      replacementObject,
    );
  }
}