list method

Future<Iterable<T>> list()

Retrieves a list of objects of type T from the directory.

This method reads all the files in the directory and filters out the files that have a '.json' extension. For each filtered file, it reads the contents, decrypts the JSON if encriptionKey is not blank, and converts it to an object of type T. The resulting objects are added to a list, which is returned at the end.

Returns a Future that completes with an Iterable of objects of type T.

Implementation

Future<Iterable<T>> list() async {
  List<T> list = [];

  if (await directory.exists()) {
    await for (var entity in directory.list()) {
      if (entity is File && entity.path.endsWith('.json')) {
        final encryptedJson = await entity.readAsString();
        final json = encriptionKey.isNotBlank ? encryptedJson.applyXorEncrypt(encriptionKey) : encryptedJson; // Decrypt the JSON if key is not blank
        final data = fromJsonFunction(jsonDecode(json));
        list.add(data);
      }
    }
  }
  return list;
}