VCardAddress.fromPlainText constructor

VCardAddress.fromPlainText(
  1. String plainText
)

Constructor from the plain text.

Exmaple: ADR;TYPE=home:;;123 Main St.;Springfield;IL;12345;USA

Implementation

factory VCardAddress.fromPlainText(String plainText) {
  if (!plainText.startsWith('ADR')) {
    throw VCardParsingError('Property "address" must starts with "ADR"');
  }
  final texts = plainText.split(':');
  VCardIdentifier? type;
  bool pref = false;
  String? aux1;
  String? aux2;
  String street;
  String city;
  String? state;
  String? zip;
  String country;
  final t1 = texts.first.split(';');
  if (t1.contains(VCardIdentifier.home.toString()) || t1.contains('TYPE=${VCardIdentifier.home.toString()}')) {
    type = VCardIdentifier.home;
  } else if (t1.contains(VCardIdentifier.work.toString()) || t1.contains('TYPE=${VCardIdentifier.work.toString()}')) {
    type = VCardIdentifier.work;
  } else if (t1.contains(VCardIdentifier.postal.toString()) ||
      t1.contains('TYPE=${VCardIdentifier.postal.toString()}')) {
    type = VCardIdentifier.postal;
  } else if (t1.contains(VCardIdentifier.parcel.toString()) ||
      t1.contains('TYPE=${VCardIdentifier.parcel.toString()}')) {
    type = VCardIdentifier.parcel;
  }
  pref = t1.contains('PREF');
  final t2 = texts[1].split(';');
  if (t2.length != 7) {
    throw VCardParsingError('This text is not recognized as a correct structure');
  }
  aux1 = t2[0].isNotEmpty ? t2[0] : null;
  aux2 = t2[1].isNotEmpty ? t2[1] : null;
  street = t2[2];
  city = t2[3];
  state = t2[4].isNotEmpty ? t2[4] : null;
  zip = t2[5].isNotEmpty ? t2[5] : null;
  country = t2[6];
  return VCardAddress(
    identifier: type,
    preferred: pref,
    auxLine1: aux1,
    auxLine2: aux2,
    street: street,
    city: city,
    state: state,
    zipCode: zip,
    country: country,
  );
}