fromJson static method

UserNotification? fromJson(
  1. dynamic value
)

Returns a new UserNotification instance and imports its values from value if it's a Map, null otherwise.

Implementation

// ignore: prefer_constructors_over_static_methods
static UserNotification? fromJson(dynamic value) {
  if (value is Map) {
    final json = value.cast<String, dynamic>();

    // Ensure that the map contains the required keys.
    // Note 1: the values aren't checked for validity beyond being non-null.
    // Note 2: this code is stripped in release mode!
    assert(() {
      assert(json.containsKey(r'_id'), 'Required key "UserNotification[_id]" is missing from JSON.');
      assert(json[r'_id'] != null, 'Required key "UserNotification[_id]" has a null value in JSON.');
      assert(json.containsKey(r'tenantId'), 'Required key "UserNotification[tenantId]" is missing from JSON.');
      assert(json[r'tenantId'] != null, 'Required key "UserNotification[tenantId]" has a null value in JSON.');
      assert(json.containsKey(r'urlId'), 'Required key "UserNotification[urlId]" is missing from JSON.');
      assert(json[r'urlId'] != null, 'Required key "UserNotification[urlId]" has a null value in JSON.');
      assert(json.containsKey(r'url'), 'Required key "UserNotification[url]" is missing from JSON.');
      assert(json[r'url'] != null, 'Required key "UserNotification[url]" has a null value in JSON.');
      assert(json.containsKey(r'relatedObjectType'), 'Required key "UserNotification[relatedObjectType]" is missing from JSON.');
      assert(json[r'relatedObjectType'] != null, 'Required key "UserNotification[relatedObjectType]" has a null value in JSON.');
      assert(json.containsKey(r'relatedObjectId'), 'Required key "UserNotification[relatedObjectId]" is missing from JSON.');
      assert(json[r'relatedObjectId'] != null, 'Required key "UserNotification[relatedObjectId]" has a null value in JSON.');
      assert(json.containsKey(r'viewed'), 'Required key "UserNotification[viewed]" is missing from JSON.');
      assert(json[r'viewed'] != null, 'Required key "UserNotification[viewed]" has a null value in JSON.');
      assert(json.containsKey(r'isUnreadMessage'), 'Required key "UserNotification[isUnreadMessage]" is missing from JSON.');
      assert(json[r'isUnreadMessage'] != null, 'Required key "UserNotification[isUnreadMessage]" has a null value in JSON.');
      assert(json.containsKey(r'sent'), 'Required key "UserNotification[sent]" is missing from JSON.');
      assert(json[r'sent'] != null, 'Required key "UserNotification[sent]" has a null value in JSON.');
      assert(json.containsKey(r'createdAt'), 'Required key "UserNotification[createdAt]" is missing from JSON.');
      assert(json[r'createdAt'] != null, 'Required key "UserNotification[createdAt]" has a null value in JSON.');
      assert(json.containsKey(r'type'), 'Required key "UserNotification[type]" is missing from JSON.');
      assert(json[r'type'] != null, 'Required key "UserNotification[type]" has a null value in JSON.');
      assert(json.containsKey(r'optedOut'), 'Required key "UserNotification[optedOut]" is missing from JSON.');
      assert(json[r'optedOut'] != null, 'Required key "UserNotification[optedOut]" has a null value in JSON.');
      return true;
    }());

    return UserNotification(
      id: mapValueOfType<String>(json, r'_id')!,
      tenantId: mapValueOfType<String>(json, r'tenantId')!,
      userId: mapValueOfType<String>(json, r'userId'),
      anonUserId: mapValueOfType<String>(json, r'anonUserId'),
      urlId: mapValueOfType<String>(json, r'urlId')!,
      url: mapValueOfType<String>(json, r'url')!,
      pageTitle: mapValueOfType<String>(json, r'pageTitle'),
      relatedObjectType: NotificationObjectType.fromJson(json[r'relatedObjectType'])!,
      relatedObjectId: mapValueOfType<String>(json, r'relatedObjectId')!,
      viewed: mapValueOfType<bool>(json, r'viewed')!,
      isUnreadMessage: mapValueOfType<bool>(json, r'isUnreadMessage')!,
      sent: mapValueOfType<bool>(json, r'sent')!,
      createdAt: mapDateTime(json, r'createdAt', r'')!,
      type: NotificationType.fromJson(json[r'type'])!,
      fromCommentId: mapValueOfType<String>(json, r'fromCommentId'),
      fromVoteId: mapValueOfType<String>(json, r'fromVoteId'),
      fromUserName: mapValueOfType<String>(json, r'fromUserName'),
      fromUserId: mapValueOfType<String>(json, r'fromUserId'),
      fromUserAvatarSrc: mapValueOfType<String>(json, r'fromUserAvatarSrc'),
      optedOut: mapValueOfType<bool>(json, r'optedOut')!,
      count: mapValueOfType<int>(json, r'count'),
      relatedIds: json[r'relatedIds'] is Iterable
          ? (json[r'relatedIds'] as Iterable).cast<String>().toList(growable: false)
          : const [],
      fromUserIds: json[r'fromUserIds'] is Iterable
          ? (json[r'fromUserIds'] as Iterable).cast<String>().toList(growable: false)
          : const [],
      fromUserNames: json[r'fromUserNames'] is Iterable
          ? (json[r'fromUserNames'] as Iterable).cast<String>().toList(growable: false)
          : const [],
    );
  }
  return null;
}