get method

  1. @override
Future<AtContact?> get(
  1. String atSign, {
  2. AtKey? getAtKey,
})

returns the AtContact.has to pass the 'atSign' Throws FormatException on invalid json Throws class extending AtException on invalid atsign.

Implementation

@override
Future<AtContact?> get(String atSign, {AtKey? getAtKey}) async {
  var contact;
  var atKey = getAtKey ?? _formKey(KeyType.contact, key: atSign, isGet: true);
  if (_regexType == RegexType.all) {
    var scanList = await atClient!.getAtKeys(regex: atKey.key);
    atKey = scanList.isNotEmpty ? _formAtKeyFromScanKeys(scanList[0]) : atKey;
  }
  var atValue = await atClient!.get(atKey);
  //check for old key if new key data is not present.
  if (atValue.value == null) {
    atKey = _formKey(KeyType.contact, key: atSign, isOld: true);
    atValue = await atClient!.get(atKey);
  }
  //migrate key to new keyformat if atKey is old.
  if (atValue.value != null && _isOldKey(atKey)) {
    var newAtKey = _formKey(KeyType.contact, key: atSign);
    await atClient!.put(newAtKey, atValue.value);
    atValue = await atClient!.get(newAtKey);
    if (atValue.value != null) await atClient!.delete(atKey);
  }
  if (atValue.value != null) {
    var value = atValue.value;
    value = value?.replaceAll('data:', '');
    if (value != null && value != 'null') {
      var json;
      try {
        json = jsonDecode(value);
      } on FormatException catch (e) {
        logger.severe('Invalid JSON. ${e.message} found in JSON : $value');
        throw InvalidSyntaxException('Invalid JSON found');
      }
      if (json != null) {
        contact = AtContact.fromJson(json);
      }
    }
  }

  return contact;
}