toMap method Null safety

Map<String, dynamic> toMap()

Implementation

Map<String, dynamic> toMap() {
  var resourceTypeStringList = <String>[];
  resourceType.forEach((type) {
    resourceTypeStringList.add(type!.toValue());
  });
  var loadTypeStringList = <String>[];
  loadType.forEach((type) {
    loadTypeStringList.add(type!.toValue());
  });

  var map = <String, dynamic>{
    "url-filter": urlFilter,
    "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
  };

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

  return map;
}