fetch method

Future<EntryObject?> fetch({
  1. required String category,
  2. required String name,
  3. bool forUpdate = false,
  4. bool isJson = false,
})

Fetches a record from the store by category and name.

Throws an AskarSessionException if the session is closed. Returns an EntryObject if the record is found, otherwise returns null.

Implementation

Future<EntryObject?> fetch({
  required String category,
  required String name,
  bool forUpdate = false,
  bool isJson = false,
}) async {
  _throwOnNullHandle("Cannot fetch from a closed session");

  EntryListHandle? entryListHandle;

  try {
    final fetchResult = await askarSessionFetch(handle!, category, name, forUpdate);

    if (!fetchResult.errorCode.isSuccess()) {
      return null;
    }

    entryListHandle = fetchResult.value;

    final entry = Entry(list: entryListHandle, position: 0);

    return entry.toJson(shouldParseValueToJson: isJson);
  } catch (e) {
    throw AskarSessionException('Failed to fetch entry: $e');
  } finally {
    if (entryListHandle != null) {
      entryListHandle.free();
    }
  }
}