set static method

dynamic set(
  1. dynamic obj,
  2. String fieldPath,
  3. dynamic value
)

Implementation

static set(obj, String fieldPath, value) {
  var paths = fieldPath.split('.');
  var curObj = obj;
  for (var i = 0; i < paths.length; i++) {
    var path = paths[i];
    var nextObj;

    if (i == paths.length - 1) {
      if (curObj is List) {
        curObj[int.parse(path)] = value;
      } else if (curObj is Map) {
        curObj[path] = value;
      } else {
        throw '[realtime] lodash set error';
      }
    } else {
      if (curObj is List) {
        nextObj = curObj[int.parse(path)] ?? {};
        curObj[int.parse(path)] = nextObj;
        curObj = nextObj;
      } else if (curObj is Map) {
        nextObj = curObj[path] ?? {};
        curObj[path] = nextObj;
        curObj = nextObj;
      } else {
        throw '[realtime] lodash set error';
      }
    }
  }
}