fromJson static method

CampaignModel? fromJson(
  1. Map<String, dynamic> json
)

Parses one campaign object from the getCampaigns response.

Returns null for entries missing required identity fields, or whose (supported) config is malformed. Unrecognised campaign types are kept as UnsupportedCampaignConfig rather than dropped, so the store mirrors the full set of campaigns the backend returned.

Config construction is delegated to CampaignConfigFactory (a registry of per-type builders) so there is no switch to grow when a type is added.

Implementation

static CampaignModel? fromJson(Map<String, dynamic> json) {
  var id = optString(json, 'id');
  if (id.isEmpty) id = optString(json, '_id');
  if (id.isEmpty) return null;

  final campaignKey = optString(json, 'campaignKey');
  if (campaignKey.isEmpty) return null;

  final campaignType = optString(json, 'campaignType');
  if (campaignType.isEmpty) return null;

  final builder = CampaignConfigFactory.builderFor(campaignType);
  // A registered builder yields a config, or `null` to drop a malformed one.
  // An unknown type is recognised-but-unsupported (kept for diagnostics).
  final config = builder == null
      ? UnsupportedCampaignConfig(
          '$campaignType rendering is not supported in the Flutter SDK')
      : builder(json);
  if (config == null) return null;

  return CampaignModel(
    id: id,
    campaignKey: campaignKey,
    campaignType: campaignType,
    config: config,
    frequency: FrequencyPolicy.fromJson(optMap(json, 'frequency')),
  );
}