getContactDetails method

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

Function to get firstname, lastname and profile picture of an atsign

Implementation

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

  if (atClientManager.atClient.getCurrentAtSign() == 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;
  List contactFields = TextStrings().contactFields;

  String? firstname;
  String? lastname;

  try {
    // firstname
    key.key = contactFields[0];
    var result = await atClientManager.atClient.get(key);
    if (result.value != null) {
      firstname = result.value;
    }
  } catch (e) {
    print("error in getting firstname: $e");
  }

  try {
    // lastname
    metadata.isPublic = true;
    metadata.namespaceAware = false;
    key.sharedBy = atSign;
    key.metadata = metadata;
    // making isPublic true (as get method changes it to false)
    key.key = contactFields[1];
    var result = await atClientManager.atClient.get(key);
    if (result.value != null) {
      lastname = result.value;
    }
  } catch (e) {
    print("error in getting lastname: $e");
  }

  if (firstname == null && lastname == null) {
    contactDetails['name'] = null;
  } else {
    // construct name
    var name = ('${firstname ?? ''} ${lastname ?? ''}').trim();
    if (name.isEmpty) {
      name = atSign.substring(1);
    }
    contactDetails['name'] = name;
  }

  try {
    // profile picture
    metadata.isPublic = true;
    metadata.namespaceAware = false;
    key.sharedBy = atSign;
    key.metadata = metadata;
    // making isPublic true (as get method changes it to false)
    key.metadata.isBinary = true;
    key.key = contactFields[2];
    Uint8List? image;

    GetRequestOptions options = GetRequestOptions();
    options.bypassCache = true;
    var result =
        await atClientManager.atClient.get(key, getRequestOptions: options);

    if (result.value != null) {
      try {
        List<int> intList = result.value.cast<int>();
        image = Uint8List.fromList(intList);
      } catch (e) {
        print('invalid iamge data: $e');
      }
    }

    contactDetails['image'] = image;
  } catch (e) {
    print("error in getting image: $e");
    contactDetails['image'] = null;
  }
  contactDetails['nickname'] = nickName != '' ? nickName : null;

  return contactDetails;
}