Advertisement
Armen_06

Untitled

Apr 20th, 2024 (edited)
437
0
22 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.73 KB | None | 0 0
  1. import 'package:flutter/foundation.dart';
  2.  
  3. @immutable
  4. class Contact {
  5.   const Contact({
  6.     required this.id,
  7.     required this.phones,
  8.     required this.emails,
  9.     required this.structuredName,
  10.     required this.organization,
  11.   });
  12.  
  13.   final String id;
  14.   final List<Phone> phones;
  15.   final List<Email> emails;
  16.   final StructuredName? structuredName;
  17.   final Organization? organization;
  18.  
  19.   String get displayName => structuredName?.displayName ?? '';
  20.  
  21.   @override
  22.   bool operator ==(Object other) =>
  23.       identical(this, other) ||
  24.       other is Contact &&
  25.           runtimeType == other.runtimeType &&
  26.           id == other.id &&
  27.           phones == other.phones &&
  28.           emails == other.emails &&
  29.           structuredName == other.structuredName &&
  30.           organization == other.organization;
  31.  
  32.   @override
  33.   int get hashCode =>
  34.       id.hashCode ^
  35.       phones.hashCode ^
  36.       emails.hashCode ^
  37.       structuredName.hashCode ^
  38.       organization.hashCode;
  39.  
  40.   @override
  41.   String toString() {
  42.     return 'Contact{id: $id, phones: $phones, emails: $emails, structuredName: $structuredName, organization: $organization}';
  43.   }
  44.  
  45.   Map<String, dynamic> toMap() {
  46.     return {
  47.       'id': id,
  48.       'phones': phones.map((e) => e.toMap()).toList(),
  49.       'emails': emails.map((e) => e.toMap()).toList(),
  50.       if (structuredName != null) 'structuredName': structuredName!.toMap(),
  51.       if (organization != null) 'organization': organization!.toMap(),
  52.     };
  53.   }
  54.  
  55.   factory Contact.fromMap(Map map) {
  56.     return Contact(
  57.       id: map['id'] ?? '',
  58.       phones:
  59.           (map['phones'] as List?)?.cast<Map>().map(Phone.fromMap).toList() ??
  60.               const [],
  61.       emails:
  62.           (map['emails'] as List?)?.cast<Map>().map(Email.fromMap).toList() ??
  63.               const [],
  64.       structuredName: map['structuredName'] != null
  65.           ? StructuredName.fromMap(map['structuredName']!)
  66.           : null,
  67.       organization: map['organization'] != null
  68.           ? Organization.fromMap(map['organization']!)
  69.           : null,
  70.     );
  71.   }
  72. }
  73.  
  74. @immutable
  75. class Phone {
  76.   Phone({
  77.     required this.number,
  78.     required this.label,
  79.   });
  80.  
  81.   String number;
  82.   final String label;
  83.  
  84.   @override
  85.   bool operator ==(Object other) =>
  86.       identical(this, other) ||
  87.       other is Phone &&
  88.           runtimeType == other.runtimeType &&
  89.           number == other.number &&
  90.           label == other.label;
  91.  
  92.   @override
  93.   int get hashCode => number.hashCode ^ label.hashCode;
  94.  
  95.   @override
  96.   String toString() {
  97.     return 'Phone{number: $number, label: $label}';
  98.   }
  99.  
  100.   Map<String, dynamic> toMap() {
  101.     return {
  102.       'number': this.number,
  103.       'label': this.label,
  104.     };
  105.   }
  106.  
  107.   factory Phone.fromMap(Map map) {
  108.     return Phone(
  109.       number: map['number'] ?? '',
  110.       label: map['label'] ?? '',
  111.     );
  112.   }
  113. }
  114.  
  115. @immutable
  116. class Email {
  117.   const Email({
  118.     required this.address,
  119.     required this.label,
  120.   });
  121.  
  122.   final String address;
  123.   final String label;
  124.  
  125.   @override
  126.   bool operator ==(Object other) =>
  127.       identical(this, other) ||
  128.       other is Email &&
  129.           runtimeType == other.runtimeType &&
  130.           address == other.address &&
  131.           label == other.label;
  132.  
  133.   @override
  134.   int get hashCode => address.hashCode ^ label.hashCode;
  135.  
  136.   @override
  137.   String toString() {
  138.     return 'Email{address: $address, label: $label}';
  139.   }
  140.  
  141.   Map<String, dynamic> toMap() {
  142.     return {
  143.       'address': this.address,
  144.       'label': this.label,
  145.     };
  146.   }
  147.  
  148.   factory Email.fromMap(Map map) {
  149.     return Email(
  150.       address: map['address'] ?? '',
  151.       label: map['label'] ?? '',
  152.     );
  153.   }
  154. }
  155.  
  156. @immutable
  157. class StructuredName {
  158.   const StructuredName({
  159.     required this.displayName,
  160.     required this.namePrefix,
  161.     required this.givenName,
  162.     required this.middleName,
  163.     required this.familyName,
  164.     required this.nameSuffix,
  165.   });
  166.  
  167.   final String displayName;
  168.   final String namePrefix;
  169.   final String givenName;
  170.   final String middleName;
  171.   final String familyName;
  172.   final String nameSuffix;
  173.  
  174.   @override
  175.   bool operator ==(Object other) =>
  176.       identical(this, other) ||
  177.       other is StructuredName &&
  178.           runtimeType == other.runtimeType &&
  179.           displayName == other.displayName &&
  180.           namePrefix == other.namePrefix &&
  181.           givenName == other.givenName &&
  182.           middleName == other.middleName &&
  183.           familyName == other.familyName &&
  184.           nameSuffix == other.nameSuffix;
  185.  
  186.   @override
  187.   int get hashCode =>
  188.       displayName.hashCode ^
  189.       namePrefix.hashCode ^
  190.       givenName.hashCode ^
  191.       middleName.hashCode ^
  192.       familyName.hashCode ^
  193.       nameSuffix.hashCode;
  194.  
  195.   @override
  196.   String toString() {
  197.     return 'StructuredName{displayName: $displayName, namePrefix: $namePrefix, givenName: $givenName, middleName: $middleName, familyName: $familyName, nameSuffix: $nameSuffix}';
  198.   }
  199.  
  200.   Map<String, dynamic> toMap() {
  201.     return {
  202.       'displayName': this.displayName,
  203.       'namePrefix': this.namePrefix,
  204.       'givenName': this.givenName,
  205.       'middleName': this.middleName,
  206.       'familyName': this.familyName,
  207.       'nameSuffix': this.nameSuffix,
  208.     };
  209.   }
  210.  
  211.   factory StructuredName.fromMap(Map map) {
  212.     return StructuredName(
  213.       displayName: map['displayName'] ?? '',
  214.       namePrefix: map['namePrefix'] ?? '',
  215.       givenName: map['givenName'] ?? '',
  216.       middleName: map['middleName'] ?? '',
  217.       familyName: map['familyName'] ?? '',
  218.       nameSuffix: map['nameSuffix'] ?? '',
  219.     );
  220.   }
  221. }
  222.  
  223. @immutable
  224. class Organization {
  225.   const Organization({
  226.     required this.company,
  227.     required this.department,
  228.     required this.jobDescription,
  229.   });
  230.  
  231.   final String company;
  232.   final String department;
  233.   final String jobDescription;
  234.  
  235.   @override
  236.   bool operator ==(Object other) =>
  237.       identical(this, other) ||
  238.       other is Organization &&
  239.           runtimeType == other.runtimeType &&
  240.           company == other.company &&
  241.           department == other.department &&
  242.           jobDescription == other.jobDescription;
  243.  
  244.   @override
  245.   int get hashCode =>
  246.       company.hashCode ^ department.hashCode ^ jobDescription.hashCode;
  247.  
  248.   @override
  249.   String toString() {
  250.     return 'Organization{company: $company, department: $department, jobDescription: $jobDescription}';
  251.   }
  252.  
  253.   Map<String, dynamic> toMap() {
  254.     return {
  255.       'company': this.company,
  256.       'department': this.department,
  257.       'jobDescription': this.jobDescription,
  258.     };
  259.   }
  260.  
  261.   factory Organization.fromMap(Map map) {
  262.     return Organization(
  263.       company: map['company'] ?? '',
  264.       department: map['department'] ?? '',
  265.       jobDescription: map['jobDescription'] ?? '',
  266.     );
  267.   }
  268. }
  269.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement