fromJson static method

User? fromJson(
  1. dynamic value
)

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

Implementation

// ignore: prefer_constructors_over_static_methods
static User? 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 "User[_id]" is missing from JSON.');
      assert(json[r'_id'] != null, 'Required key "User[_id]" has a null value in JSON.');
      assert(json.containsKey(r'username'), 'Required key "User[username]" is missing from JSON.');
      assert(json[r'username'] != null, 'Required key "User[username]" has a null value in JSON.');
      assert(json.containsKey(r'email'), 'Required key "User[email]" is missing from JSON.');
      assert(json.containsKey(r'signUpDate'), 'Required key "User[signUpDate]" is missing from JSON.');
      assert(json[r'signUpDate'] != null, 'Required key "User[signUpDate]" has a null value in JSON.');
      assert(json.containsKey(r'createdFromTenantId'), 'Required key "User[createdFromTenantId]" is missing from JSON.');
      assert(json.containsKey(r'createdFromIpHashed'), 'Required key "User[createdFromIpHashed]" is missing from JSON.');
      assert(json[r'createdFromIpHashed'] != null, 'Required key "User[createdFromIpHashed]" has a null value in JSON.');
      assert(json.containsKey(r'verified'), 'Required key "User[verified]" is missing from JSON.');
      assert(json[r'verified'] != null, 'Required key "User[verified]" has a null value in JSON.');
      assert(json.containsKey(r'loginId'), 'Required key "User[loginId]" is missing from JSON.');
      assert(json[r'loginId'] != null, 'Required key "User[loginId]" has a null value in JSON.');
      assert(json.containsKey(r'loginIdDate'), 'Required key "User[loginIdDate]" is missing from JSON.');
      assert(json[r'loginIdDate'] != null, 'Required key "User[loginIdDate]" has a null value in JSON.');
      return true;
    }());

    return User(
      id: mapValueOfType<String>(json, r'_id')!,
      tenantId: mapValueOfType<String>(json, r'tenantId'),
      username: mapValueOfType<String>(json, r'username')!,
      displayName: mapValueOfType<String>(json, r'displayName'),
      websiteUrl: mapValueOfType<String>(json, r'websiteUrl'),
      email: mapValueOfType<String>(json, r'email'),
      pendingEmail: mapValueOfType<String>(json, r'pendingEmail'),
      backupEmail: mapValueOfType<String>(json, r'backupEmail'),
      pendingBackupEmail: mapValueOfType<String>(json, r'pendingBackupEmail'),
      signUpDate: mapValueOfType<int>(json, r'signUpDate')!,
      createdFromUrlId: mapValueOfType<String>(json, r'createdFromUrlId'),
      createdFromTenantId: mapValueOfType<String>(json, r'createdFromTenantId'),
      createdFromIpHashed: mapValueOfType<String>(json, r'createdFromIpHashed')!,
      verified: mapValueOfType<bool>(json, r'verified')!,
      loginId: mapValueOfType<String>(json, r'loginId')!,
      loginIdDate: mapValueOfType<int>(json, r'loginIdDate')!,
      loginCount: mapValueOfType<int>(json, r'loginCount'),
      optedInNotifications: mapValueOfType<bool>(json, r'optedInNotifications'),
      optedInTenantNotifications: mapValueOfType<bool>(json, r'optedInTenantNotifications'),
      hideAccountCode: mapValueOfType<bool>(json, r'hideAccountCode'),
      avatarSrc: mapValueOfType<String>(json, r'avatarSrc'),
      isFastCommentsHelpRequestAdmin: mapValueOfType<bool>(json, r'isFastCommentsHelpRequestAdmin'),
      isHelpRequestAdmin: mapValueOfType<bool>(json, r'isHelpRequestAdmin'),
      isAccountOwner: mapValueOfType<bool>(json, r'isAccountOwner'),
      isAdminAdmin: mapValueOfType<bool>(json, r'isAdminAdmin'),
      isBillingAdmin: mapValueOfType<bool>(json, r'isBillingAdmin'),
      isAnalyticsAdmin: mapValueOfType<bool>(json, r'isAnalyticsAdmin'),
      isCustomizationAdmin: mapValueOfType<bool>(json, r'isCustomizationAdmin'),
      isManageDataAdmin: mapValueOfType<bool>(json, r'isManageDataAdmin'),
      isCommentModeratorAdmin: mapValueOfType<bool>(json, r'isCommentModeratorAdmin'),
      isAPIAdmin: mapValueOfType<bool>(json, r'isAPIAdmin'),
      isSiteAdmin: mapValueOfType<bool>(json, r'isSiteAdmin'),
      moderatorIds: json[r'moderatorIds'] is Iterable
          ? (json[r'moderatorIds'] as Iterable).cast<String>().toList(growable: false)
          : const [],
      isImpersonator: mapValueOfType<bool>(json, r'isImpersonator'),
      isCouponManager: mapValueOfType<bool>(json, r'isCouponManager'),
      locale: mapValueOfType<String>(json, r'locale'),
      digestEmailFrequency: DigestEmailFrequency.fromJson(json[r'digestEmailFrequency']),
      notificationFrequency: mapValueOfType<double>(json, r'notificationFrequency'),
      adminNotificationFrequency: mapValueOfType<double>(json, r'adminNotificationFrequency'),
      agentApprovalNotificationFrequency: ImportedAgentApprovalNotificationFrequency.fromJson(json[r'agentApprovalNotificationFrequency']),
      lastTenantNotificationSentDate: mapDateTime(json, r'lastTenantNotificationSentDate', r''),
      lastReplyNotificationSentDate: mapDateTime(json, r'lastReplyNotificationSentDate', r''),
      ignoredAddToMySiteMessages: mapValueOfType<bool>(json, r'ignoredAddToMySiteMessages'),
      lastLoginDate: mapDateTime(json, r'lastLoginDate', r''),
      displayLabel: mapValueOfType<String>(json, r'displayLabel'),
      isProfileActivityPrivate: mapValueOfType<bool>(json, r'isProfileActivityPrivate'),
      isProfileCommentsPrivate: mapValueOfType<bool>(json, r'isProfileCommentsPrivate'),
      isProfileDMDisabled: mapValueOfType<bool>(json, r'isProfileDMDisabled'),
      profileCommentApprovalMode: mapValueOfType<double>(json, r'profileCommentApprovalMode'),
      karma: mapValueOfType<double>(json, r'karma'),
      passwordHash: mapValueOfType<String>(json, r'passwordHash'),
      averageTicketAckTimeMS: mapValueOfType<double>(json, r'averageTicketAckTimeMS'),
      hasBlockedUsers: mapValueOfType<bool>(json, r'hasBlockedUsers'),
      bio: mapValueOfType<String>(json, r'bio'),
      headerBackgroundSrc: mapValueOfType<String>(json, r'headerBackgroundSrc'),
      countryCode: mapValueOfType<String>(json, r'countryCode'),
      countryFlag: mapValueOfType<String>(json, r'countryFlag'),
      socialLinks: json[r'socialLinks'] is Iterable
          ? (json[r'socialLinks'] as Iterable).cast<String>().toList(growable: false)
          : const [],
      hasTwoFactor: mapValueOfType<bool>(json, r'hasTwoFactor'),
      isEmailSuppressed: mapValueOfType<bool>(json, r'isEmailSuppressed'),
    );
  }
  return null;
}