setPath method

Snapshot setPath(
  1. String path,
  2. dynamic value, {
  3. bool createParents = true,

Returns a snapshot with updated content at path.

Unmodified children and grandchildren are recycled. So, also their conversions are reused.

value may either be a JSON-like object or a Snapshot.

When the updated value equals the old value, this snapshot will be returned. In case the value argument was a compatible (i.e. with same decoder) Snapshot, the cache of the argument will be merged into this snapshot.

When value is a compatible snapshot (and the content changed), the returned snapshot will contain value in its cache.

When createParents is true (default) and some of the parents do not exist or are not of type Map or List, they are created (as a Map). When createParents is false an error will be thrown.


Snapshot setPath(String path, dynamic value, {bool createParents = true}) {
  var pointer =
      JsonPointer.fromString(path.startsWith('/') ? path : '/$path');

  if (pointer.segments.isEmpty) return set(value);

  var content = value is Snapshot ? value.value : value;
  var newContent = _setPathInContent(this.value, pointer.segments, content,
      createParents: createParents);

  var snapshot = Snapshot.fromJson(newContent, decoder: decoder);

  if (value is Snapshot && value.decoder == decoder) {
    var parent = snapshot.child(pointer.parent.toString());
    (parent as _SnapshotImpl)._childrenCache[pointer.segments.last] = value;
  return set(snapshot);