getContactDetails function

Future<Map<String, dynamic>> getContactDetails(
  1. dynamic atSign
)

Implementation

Future<Map<String, dynamic>> getContactDetails(atSign) async {
  var contactDetails = <String, dynamic>{};

  if (KeyStreamService().atClientInstance == null || atSign == null) {
    return contactDetails;
  } else if (!atSign.contains('@')) {
    atSign = '@' + atSign;
  }
  var metadata = Metadata();
  metadata.isPublic = true;
  metadata.namespaceAware = false;
  var key = AtKey();
  key.sharedBy = atSign;
  key.metadata = metadata;
  var contactFields = [
    'firstname.persona',
    'lastname.persona',
    'image.persona',
  ];

  try {
    // firstname
    key.key = contactFields[0];
    var result = await KeyStreamService().atClientInstance!.get(key).catchError(
        // ignore: return_of_invalid_type_from_catch_error
        (e) => print('error in get ${e.errorCode} ${e.errorMessage}'));
    var firstname = result.value;

    // lastname
    key.key = contactFields[1];
    result = await KeyStreamService().atClientInstance!.get(key);
    var lastname = result.value;

    // construct name
    var name = ((firstname ?? '') + ' ' + (lastname ?? '')).trim();
    if (name.length == 0) {
      name = atSign.substring(1);
    }

    // profile picture
    key.metadata!.isBinary = true;
    key.key = contactFields[2];
    result = await KeyStreamService().atClientInstance!.get(key);
    var image = result.value;
    contactDetails['name'] = name;
    contactDetails['image'] = image;
  } catch (e) {
    contactDetails['name'] = null;
    contactDetails['image'] = null;
  }
  return contactDetails;
}