Better explain how `getSingle` and `watchSingle` works.

This commit is contained in:
Simon Binder 2019-08-24 18:03:53 +02:00
parent 89f3987e6b
commit ca76746000
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
1 changed files with 18 additions and 2 deletions

View File

@ -79,8 +79,21 @@ abstract class Selectable<T> {
/// result too many values, this method will throw. If no row is returned,
/// `null` will be returned instead.
///
/// {@template moor_single_query_expl}
/// Be aware that this operation won't put a limit clause on this statement,
/// if that's needed you would have to do that yourself.
/// if that's needed you would have to do use [SimpleSelectStatement.limit]:
/// ```dart
/// Future<TodoEntry> loadMostImportant() {
/// return (select(todos)
/// ..orderBy([(t) => OrderingTerm(expression: t.priority, mode: OrderingMode.desc)])
/// ..limit(1)
/// ).getSingle();
/// }
/// ```
/// You should only use this method if you know the query won't have more than
/// one row, for instance because you used `limit(1)` or you know the `where`
/// clause will only allow one row.
/// {@endtemplate}
Future<T> getSingle() async {
final list = await get();
final iterator = list.iterator;
@ -100,7 +113,10 @@ abstract class Selectable<T> {
/// However, it is assumed that the query will only emit one result, so
/// instead of returning a [Stream<List<T>>], this returns a [Stream<T>]. If
/// the query emits more than one row at some point, an error will be emitted
/// to the stream instead.
/// to the stream instead. If the query emits zero rows at some point, `null`
/// will be added to the stream instead.
///
/// {@macro moor_single_query_expl}
Stream<T> watchSingle() {
return watch().transform(singleElements());
}