putExternalId method

RudderOption putExternalId(
  1. String type,
  2. String id
)

Implementation

RudderOption putExternalId(String type, String id) {
  externalIds ??= [];

  Map<String, String>? externalIdMap;
  int mapIndex = -1;
  for (int index = 0; index < externalIds!.length; index++) {
    Map<String, String> map = externalIds!.elementAt(index);
    String mapType = map["type"].toString();
    if (Utils.equalsIgnoreCase(mapType, type)) {
      externalIdMap = map;
      mapIndex = index;
      break;
    }
  }

  // if not present from previous runs: create new and assign the type
  if (externalIdMap == null) {
    externalIdMap = {};
    externalIdMap["type"] = type;
  }

  // assign new id or update existing id
  externalIdMap["id"] = id;

  // finally update existing position or add new id
  if (mapIndex == -1) {
    // not found in existing storage
    externalIds!.add(externalIdMap);
  } else {
    externalIds!.elementAt(mapIndex)["id"] = id;
  }

  // return for builder pattern
  return this;
}