mapToSingleOrNull

Maps the first row of this KQLiteCursor into an object of type T using mapper, or returns null if the cursor is empty.

This is a terminal operation that expects at most one row in the result set.

Cursor lifecycle

  • The KQLiteCursor is automatically closed after execution, regardless of whether the mapping succeeds or returns null.

  • This uses use to ensure resource safety.

Characteristics

  • Lenient: Returns null if the cursor is empty (no rows).

  • Lenient: Returns null if the cursor has more than one row.

  • Safe: Ensures the cursor is closed immediately after the mapping attempt.

Return

The single mapped element of type T, or null if the cursor is empty.

Author

MOHAMMAD AZIM ANSARI

Parameters

mapper

Transforms the cursor row into T.

See also


fun <T> Flow<KQLiteCursor>.mapToSingleOrNull(dispatcher: CoroutineDispatcher? = null, mapper: (KQLiteCursor) -> T): Flow<T?>

Maps each emitted KQLiteCursor from the upstream Flow into an object of type T or null.

Each cursor emission is expected to contain at most one row. The cursor is fully consumed and automatically closed after reading.

Cursor lifecycle

  • The KQLiteCursor is automatically closed after execution, regardless of whether the mapping succeeds, returns null, or an exception is thrown during mapping.

  • This uses use internally to ensure resource safety.

Threading

  • Mapping is executed on Dispatchers.IO by default.

  • A custom dispatcher can be provided to control threading.

  • Uses flowOn to shift upstream execution.

Characteristics

  • Lenient: Returns null if the cursor is empty (no rows).

  • Lenient: Returns null if the cursor has more than one row.

  • Safe: Ensures the cursor is closed immediately after the mapping attempt.

Return

A flow emitting an object of type T, or null if the cursor is empty or has multiple rows.

Author

MOHAMMAD AZIM ANSARI

Parameters

dispatcher

Optional CoroutineDispatcher for upstream execution (defaults to Dispatchers.IO).

mapper

Transforms the cursor row into T.

See also