listGroupNames method

  1. @override
Future<List<String?>> listGroupNames()

fetches all the group names as a list on success return List of Group names otherwise []

Implementation

@override
Future<List<String?>> listGroupNames() async {
  var atKey = _formKey(KeyType.groupList, isGet: true);
  if (_regexType == RegexType.all) {
    var scanList = await atClient!.getAtKeys(regex: atKey.key);
    atKey = scanList.isNotEmpty ? _formAtKeyFromScanKeys(scanList[0]) : atKey;
  }
  AtValue? result;
  result = await _get(atKey);
  //check for old key if new key data is not present.
  if (result?.value == null) {
    atKey = _formKey(KeyType.groupList, isOld: true);
    result = await _get(atKey);
  }
  //migrate key to new keyformat.
  if (result?.value != null && _isOldKey(atKey)) {
    var newAtKey = _formKey(
      KeyType.groupList,
    );
    await atClient!.put(newAtKey, result?.value);
    AtValue? getValue;
    getValue = await _get(newAtKey);
    // If new key is stored successfully, remove the old key.
    if (getValue?.value != null) await atClient!.delete(atKey);
  }
  // get name from AtGroupBasicInfo for all the groups.
  List<dynamic>? list = [];
  list = (result?.value != null) ? jsonDecode(result?.value) : [];
  list = List<String>.from(list!);
  var groupNames = <String?>[];
  list.forEach((group) {
    var groupInfo = AtGroupBasicInfo.fromJson(jsonDecode(group));
    groupNames.add(groupInfo.atGroupName);
  });
  return groupNames;
}