set method

void set(
  1. dynamic newValue, {
  2. bool notify = true,
})

Implementation

void set(dynamic newValue, {bool notify = true}) {
  if (newValue is Data) throw Exception('Cannot set RecipeValue as value');

  bool disableNotify = false;

  if (newValue is Map) {
    if (_value is! Map) throw Exception('Cannot update not a map');
    (newValue as Map<String, dynamic>).forEach((key, subValue) {
      if (subValue is! Data) {
        throw Exception('Cannot update not a RecipeValue : $subValue');
      }
      if ((_value as Map).containsKey(key)) {
        if ((_value as Map)[key] == null) {
          (_value as Map)[key] = Data(null);
          (_value as Map)[key]!.parent = this;
        }
        (_value as Map)[key]!.set(subValue.value, notify: notify);
        disableNotify = true;
      } else {
        subValue.addParent(this);
        (_value as Map)[key] = subValue;
      }
    });
    if (disableNotify) {
      notify = false;
    }
  } else if (newValue is List) {
    if (_value is! List) throw Exception('Cannot update not a list');
    (_value as List).clear();
    for (var subValue in newValue) {
      if (subValue is! Data) {
        throw Exception('Cannot update not a RecipeValue : $subValue');
      }
      (_value as List).add(subValue);
    }
  } else {
    _value = newValue;
  }

  if (notify) notifyListeners();
}