getListLike method

Future<List<Map<String, dynamic>>?> getListLike(
  1. String key
)

Function to retrieve a list of objects from the database stored under a similar key. example: Message list could be retrieved like this await jsonStore.getListLike('message%'); //this should return a list based on the following data | key | value | | message-1 | ... | | message-2 | ... | | message-3 | ... |

Implementation

Future<List<Map<String, dynamic>>?> getListLike(String key) async {
  final Database db = await _databaseFuture;

  final List<Map<String, dynamic>> queryResult =
      await db.query(_table, where: 'key like ?', whereArgs: [key]);

  if (queryResult.isNotEmpty) {
    List<Map<String, dynamic>> result = [];
    await Future.forEach(queryResult, (Map<String, dynamic> row) async {
      final Map<String, dynamic> metadata = json.decode(row['metadata']);
      final String value = row['value'];
      final DateTime lastUpdated =
          DateTime.fromMillisecondsSinceEpoch(row['lastUpdated'] as int);
      final timeLapsed = DateTime.now().millisecondsSinceEpoch -
          lastUpdated.millisecondsSinceEpoch;
      final ttl = metadata[_timeToLiveKey];
      if (ttl != null && timeLapsed > (ttl as int)) {
        await db.delete(_table, where: 'key like ?', whereArgs: [key]);
        return null;
      } else {
        final encrypted = metadata[_encryptedKey] as bool;
        if (encrypted && metadata[_ivKey] != null) {
          final IV iv = IV.fromBase64(metadata[_ivKey]);
          result.add(
            await _decodeJson(value, encrypted, iv),
          );
        } else {
          result.add(
            await _decodeJson(value, encrypted, null),
          );
        }
      }
    });
    return result;
  }
  return null;
}