getUtf8 method

Future<String?> getUtf8(
  1. Pointer<MDB_txn> txn,
  2. String key, {
  3. String? dbName,
})

Retrieves a UTF-8 encoded string value from the database

The key is used as UTF-8 encoded database key.

The optional dbName parameter specifies a named database. If not provided, the default database will be used.

Returns the decoded UTF-8 string value, or null if the key doesn't exist.

Example:

final txn = await db.txnStart(flags: LMDBFlagSet()..add(MDB_RDONLY));
try {
  final json = await db.getUtf8(txn, 'user_123');
  if (json != null) {
    final userData = jsonDecode(json);
    print('User name: ${userData['name']}');
  }
  await db.txnCommit(txn);
} catch (e) {
  await db.txnAbort(txn);
  rethrow;
}

Throws StateError if the database is closed. Throws LMDBException if the operation fails. Throws FormatException if the stored data is not valid UTF-8.

Implementation

Future<String?> getUtf8(
  Pointer<MDB_txn> txn,
  String key, {
  String? dbName,
}) async {
  final result = await get(txn, key, dbName: dbName);
  return result != null ? utf8.decode(result) : null;
}