toMap method

Map<String, dynamic> toMap({
  1. EnumMethod? enumMethod,
})

Implementation

Map<String, dynamic> toMap({EnumMethod? enumMethod}) {
  List<String> resourceTypeStringList = [];
  resourceType.forEach((type) {
    switch (enumMethod ?? EnumMethod.nativeValue) {
      case EnumMethod.nativeValue:
        if (type.isSupported()) {
          resourceTypeStringList.add(type.toNativeValue()!);
        }
        break;
      case EnumMethod.value:
        resourceTypeStringList.add(type.toValue());
        break;
      case EnumMethod.name:
        resourceTypeStringList.add(type.name());
        break;
    }
  });
  List<String> loadTypeStringList = [];
  loadType.forEach((type) {
    switch (enumMethod ?? EnumMethod.nativeValue) {
      case EnumMethod.nativeValue:
        if (type.isSupported()) {
          loadTypeStringList.add(type.toNativeValue()!);
        }
        break;
      case EnumMethod.value:
        loadTypeStringList.add(type.toValue());
        break;
      case EnumMethod.name:
        loadTypeStringList.add(type.name());
        break;
    }
  });
  List<String> loadContextStringList = [];
  loadContext.forEach((type) {
    switch (enumMethod ?? EnumMethod.nativeValue) {
      case EnumMethod.nativeValue:
        if (type.isSupported()) {
          loadContextStringList.add(type.toNativeValue()!);
        }
        break;
      case EnumMethod.value:
        loadContextStringList.add(type.toValue());
        break;
      case EnumMethod.name:
        loadContextStringList.add(type.name());
        break;
    }
  });

  Map<String, dynamic> map = {
    "url-filter": urlFilter,
    "if-frame-url": ifFrameUrl,
    "url-filter-is-case-sensitive": urlFilterIsCaseSensitive,
    "if-domain": ifDomain,
    "unless-domain": unlessDomain,
    "resource-type": resourceTypeStringList,
    "load-type": loadTypeStringList,
    "if-top-url": ifTopUrl,
    "unless-top-url": unlessTopUrl,
    "load-context": loadContextStringList,
  };

  map.keys
      .where(
        (key) =>
            map[key] == null ||
            (map[key] is List && (map[key] as List).length == 0),
      ) // filter keys
      .toList() // create a copy to avoid concurrent modifications
      .forEach(map.remove);

  return map;
}