getUtf8Auto method

Future<String?> getUtf8Auto(
  1. String key, {
  2. String? dbName,
})

Retrieves a UTF-8 string with automatic transaction management.

Uses an automatic read-only transaction.

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.

This method handles the transaction automatically, including commit and abort in case of errors.

Example:

// Read simple string
final greeting = await db.getUtf8Auto('greeting');
print(greeting); // Prints: Hello, World!

// Read and parse JSON data
final jsonStr = await db.getUtf8Auto('user_123');
if (jsonStr != null) {
  final userData = jsonDecode(jsonStr);
  print('User name: ${userData['name']}');
}

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?> getUtf8Auto(
  String key, {
  String? dbName,
}) async {
  return _withTransaction((txn) async {
    return getUtf8(txn, key, dbName: dbName);
  }, flags: LMDBFlagSet.readOnly);
}