getParsedDate method

PassportData? getParsedDate()

Implementation

PassportData? getParsedDate() {
  ///"PMCHEPHILLIPS<<AGNES<<<<<<<<<<<<<<<<<<<<<<<<X3600984<3CHE5011106F2404071<<<<<<<<<<<<<<<4"
  ///"PCAZEHUSEYNLI<<ORKHAN<<<<<<<<<<<<<<<<<<<<<<<\^PX110003442AZE7503153M230801030LJV5Z<<<<<<<<…>"
  try {
    String val = ocTrackData.map((e) => e.value).join("");
  String type = "Unknown";
  String passType = "Normal";
  String country = "";
  String firstname = "";
  String lastname = "";
  String passportNumber = "";
  String nationality = "";
  String birthDate = "";
  String sex = "Unspecified";
  String expireDate = "";
  String personalNumber = "";


  String firstRow = val.substring(0, 44);
  String secondRow = val.substring(0, 88).replaceFirst(firstRow, "");

  String firstChar = val.substring(0, 1);
  if (firstChar.toLowerCase() == "p") {
    type = "Passport";
  } else if (firstChar == "V") {
    type = "Visa";
  } else if (firstChar == "I" || firstChar == "A" || firstChar == "C") {
    type = "Official Travel Document";
  }

  String secondChar = val.substring(1, 2);
  if (secondChar != "<") {
    passType = secondChar;
  }

  country = val.substring(2, 5);

  String nameSection = val.substring(5, 44);
  String surname = nameSection
      .split("<<")
      .first;
  String givenNamePart = nameSection.replaceFirst("$surname<<", "");
  String givenName = givenNamePart.split("<").join(" ");
  lastname = surname;
  firstname = givenName.replaceAll("  ", "").replaceAll("<", "");;

  passportNumber = secondRow.substring(0, 9);
  nationality = secondRow.substring(10, 13);
  birthDate = secondRow.substring(13, 19);
  sex = secondRow.substring(20, 21) != "<" ? secondRow.substring(20, 21) : sex;
  expireDate = secondRow.substring(21, 27);
  personalNumber = secondRow.substring(28, 42).replaceAll("<", "");

  birthDate = birthDate.substring(0,2)+"-"+birthDate.substring(2,4)+"-"+birthDate.substring(4,6);
  expireDate = expireDate.substring(0,2)+"-"+expireDate.substring(2,4)+"-"+expireDate.substring(4,6);


    DateTime birthDateTime = DateFormat("yy-MM-dd").parse(birthDate);
    DateTime expireDateTime = DateFormat("yy-MM-dd").parse(expireDate);

    birthDate =DateFormat("yyyy-MM-dd").format(birthDateTime);
    expireDate =DateFormat("yyyy-MM-dd").format(expireDateTime);
  Map<String,dynamic> passJson =  {
    "Type": type,
    "FirstName": firstname,
    "LastName": lastname,
    "PassportType": passType,
    "Country": country,
    "PassportNumber": passportNumber,
    "Nationality": nationality,
    "BirthDate": birthDate,
    "Sex": sex,
    "ExpireDate": expireDate,
    "PersonalNumber": personalNumber,
    "RawData": val,
  };
  return PassportData.fromJson(passJson);

  }catch (e) {
    String val = ocTrackData.map((e) => e.value).join("");
    log("$e");
    return null;
  }



}