Returning values from streams

71 views
Skip to first unread message

Malibu

unread,
Mar 22, 2023, 11:02:45 AM3/22/23
to Dart Misc
Hi there, there are many things about streams that are still perplexing to me, for example:

Future<AuthError?> connect(String user, String pw) {

    connectStream.take(1).listen((_) { return null; }, onError: (error) { return error;});
    
    return resultOfStream?
}

What is the accepted way to return null from the function if there is a single result or the error if there is an error?  I know I can return await Completer().future at the end of the function but that seems rather messy.  Is that the only way?

I was trying to figure out how to rework the stream so that it put null into a Future on a good result and put the error into a future on a bad result and return the result of the connect stream directly but couldn't figure that out.

Malibu

unread,
Mar 22, 2023, 12:14:13 PM3/22/23
to Dart Misc, Malibu
To clarify, this is basically what I want to accomplish but without using a completer:

Future<AuthError?> connect(String user, String pw) async {
    connectCompletion = Completer()
    connectStream.take(1).listen((_) { connectCompletion.complete(null); }, onError: (error) { connectCompletion.complete(error);});
    return connectCompletion.future

Bob Nystrom

unread,
Mar 22, 2023, 1:39:20 PM3/22/23
to mi...@dartlang.org, Malibu
I'm not an expert on streams, but I would try:

Future<AuthError?> connect(String user, String pw) async {
  try {
    await connectStream.first;
  } on AuthError catch (error) {
    return error;
  }
 
  // If we get here, we got a value.
  return null;
}


– bob

--
For more ways to connect visit https://dart.dev/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/6b3e5b4f-8baf-4d25-b783-b06e7ea12cd4n%40dartlang.org.

Bob Nystrom

unread,
Mar 22, 2023, 4:40:55 PM3/22/23
to Malibu, Dart Misc
Stream.first returns a Future and it's specified that any errors produced by the stream will be routed through that returned Future.

– bob 

On Wed, Mar 22, 2023 at 11:27 AM Malibu <mali...@gmail.com> wrote:
Yes, that worked.  Thank you!

So I had tried the same thing but with:

await connectStream.listen( (_) { } );

instead of:

await connectStream.first;

But in my case, the exception did not propagate up to the try/catch block.  It stayed within the stream and reported an uncaught exception.  Do you know why the .first causes different exception handling?

Malibu

unread,
Mar 23, 2023, 6:35:05 AM3/23/23
to Dart Misc, Bob Nystrom, Malibu
Yes, that worked.  Thank you!

So I had tried the same thing but with:

await connectStream.listen( (_) { } );

instead of:

await connectStream.first;

But in my case, the exception did not propagate up to the try/catch block.  It stayed within the stream and reported an uncaught exception.  Do you know why the .first causes different exception handling?

On Wednesday, 22 March 2023 at 14:39:20 UTC-3 Bob Nystrom wrote:

Malibu

unread,
Mar 23, 2023, 6:35:05 AM3/23/23
to Dart Misc, Bob Nystrom, Dart Misc, Malibu
Ach, I read the page on Futures and Error handling five times and it only talks about future functions for error handling.  It would have been nice if they started with try-catch on a future.  But that is on the page about 'Asyncrony support'.  When I read 'errors will be routed through that returned future' I thought that meant you had to use a future function.

So I guess whenever you want an exception from a stream to propagate to the outside you need to end the stream with a future somehow.

Thank you for helping me learn this.
Reply all
Reply to author
Forward
0 new messages