diff --git a/drift/lib/src/utils/single_transformer.dart b/drift/lib/src/utils/single_transformer.dart index 98454d18..3eff83ee 100644 --- a/drift/lib/src/utils/single_transformer.dart +++ b/drift/lib/src/utils/single_transformer.dart @@ -4,16 +4,17 @@ import 'dart:async'; /// that each list is a singleton or empty. StreamTransformer, T?> singleElementsOrNull() { return StreamTransformer.fromHandlers(handleData: (data, sink) { + T? result; + try { - if (data.isEmpty) { - sink.add(null); - } else { - sink.add(data.single); + if (data.isNotEmpty) { + result = data.single; } } catch (e) { - sink.addError( - StateError('Expected exactly one element, but got ${data.length}')); + throw StateError('Expected exactly one element, but got ${data.length}'); } + + sink.add(result); }); } @@ -21,11 +22,14 @@ StreamTransformer, T?> singleElementsOrNull() { /// that each list is a singleton. StreamTransformer, T> singleElements() { return StreamTransformer.fromHandlers(handleData: (data, sink) { + T single; + try { - sink.add(data.single); + single = data.single; } catch (e) { - sink.addError( - StateError('Expected exactly one element, but got ${data.length}')); + throw StateError('Expected exactly one element, but got ${data.length}'); } + + sink.add(single); }); } diff --git a/drift/test/utils/single_transformer_test.dart b/drift/test/utils/single_transformer_test.dart index 4542dde8..091291de 100644 --- a/drift/test/utils/single_transformer_test.dart +++ b/drift/test/utils/single_transformer_test.dart @@ -22,8 +22,15 @@ void main() { final controller = StreamController>(); final stream = controller.stream.transform(singleElements()); - expectLater(stream, - emitsInOrder([1, emitsError(anything), 2, emitsError(anything)])); + expectLater( + stream, + emitsInOrder([ + 1, + emitsError(_stateErrorWithTrace), + 2, + emitsError(_stateErrorWithTrace) + ]), + ); controller ..add([1]) @@ -38,3 +45,6 @@ void main() { expect(stream.transform(singleElementsOrNull()), emits(isNull)); }); } + +Matcher _stateErrorWithTrace = + isStateError.having((e) => e.stackTrace, 'stackTrace', isNotNull);