KnotEvent.fromJson constructor

KnotEvent.fromJson(
  1. dynamic json
)

Factory method to create a KnotEvent from a json map.

Implementation

factory KnotEvent.fromJson(dynamic json) {
  // Ensure json is a Map and safely cast it
  if (json is! Map) {
    throw ArgumentError('Expected a Map<String, dynamic>, got ${json.runtimeType}');
  }

  final Map<String, Object?> jsonMap = json.cast<String, Object?>();

  String? merchant = jsonMap["merchant"] as String?;
  String? merchantId = jsonMap["merchantId"] as String?;
  String event = jsonMap["event"] as String;
  String? taskId = jsonMap["taskId"] as String?;
  dynamic rawMetaData = jsonMap["metaData"];
  String environment = jsonMap["environment"] as String;
  String? product = jsonMap["product"] as String?;
  Map<String, Object?> metaData;

  if (rawMetaData is Map) {
    metaData = rawMetaData.cast<String, Object?>();
  } else {
    metaData = {};
  }

  return KnotEvent(
      merchant: merchant,
      merchantId: merchantId,
      environment: environment,
      event: event,
      product: product,
      taskId: taskId,
      metaData: metaData
  );
}