addObject method

Future<bool> addObject(
  1. Map<String, dynamic> addedObject
)

Adds an object to the current opened box

addedObject : A Map<String, dynamic> representing the new object to be added. Returns a Future

Implementation

Future<bool> addObject(Map<String, dynamic> addedObject) async {
  // final currentBox = _hiveViewState.currentOpenedBox!;
  // try {
  //   final fromJson = _hiveViewState.boxesMap[currentBox]!;
  //   final newAddedObject = fromJson(addedObject);
  //   await currentBox.put(addedObject["id"], newAddedObject);
  //   return true;
  // } catch (e) {
  //   _errorCallback(e.toString());
  //   return false;
  // }

  try {
    bool isThereBoxes = _hiveViewState.boxesMap.entries.isNotEmpty;
    bool isFoundOpenBox = _hiveViewState.currentOpenedBox != null;
    bool isHaveIdProperty =
        addedObject.keys.any((element) => element == "id");
    if (!isThereBoxes) {
      _errorCallback("there is Not Boxes");
      return false;
    } else if (!isFoundOpenBox) {
      _errorCallback("there is Not Open Box");
      return false;
    } else if (!isHaveIdProperty) {
      _errorCallback("there is Not ID  Property \n add {\"id\":(888 as id)}");
      return false;
    } else {
      final currentBox = _hiveViewState.currentOpenedBox!;

      final fromJson = _hiveViewState.boxesMap[currentBox]!;
      final newAddedObject = fromJson(addedObject);
      await currentBox.put(addedObject["id"], newAddedObject);
      return true;
    }
  } catch (e) {
    _errorCallback(e.toString());
    return false;
  }
}