Ride Sun
Ride Sun

Reputation: 2343

How can I add a timeout to a dart stream

I would like to get a timeout when I listen to a stream but don't receive a value after a duration. Here is a example how it could look like:

dtcStatus.valueStream.stream.listen
   ((event) {
         do Something with the event
   }, 
   timeoutDuration: Duration(milliseconds: 300), 
   onTimeout: () {
    do something on the timeout here
   }
);

could that be archived with an extension?

Upvotes: 2

Views: 2824

Answers (1)

Randal Schwartz
Randal Schwartz

Reputation: 44066

A method on Stream:

timeout(Duration timeLimit, {void onTimeout(EventSink<T> sink)}) → Stream<T>
Creates a new stream with the same events as this stream. [...]

should be exactly what you want. You even had the syntax nearly right!

Upvotes: 3

Related Questions