Handling Streams
When you need to get values from a Stream,you have two options:
- Use
async
and an asynchronous for loop (await for
). - Use the Stream API, as describedin the library tour.
Note: Before using await for
, be sure that it makes the code clearer and that you really do want to wait for all of the stream’s results. For example, you usually should not use await for
for UI event listeners, because UI frameworks send endless streams of events.
An asynchronous for loop has the following form:
await for (varOrType identifier in expression) {
// Executes each time the stream emits a value.
}
The value of expression
must have type Stream.Execution proceeds as follows:
- Wait until the stream emits a value.
- Execute the body of the for loop,with the variable set to that emitted value.
- Repeat 1 and 2 until the stream is closed.To stop listening to the stream,you can use a
break
orreturn
statement,which breaks out of the for loopand unsubscribes from the stream.
If you get a compile-time error when implementing an asynchronous for loop,make sure the await for
is in an async
function.For example, to use an asynchronous for loop in your app’s main()
function,the body of main()
must be marked as async
:
- Future main() async {
- // ...
- await for (var request in requestServer) {
- handleRequest(request);
- }
- // ...
- }
For more information about asynchronous programming, in general, see thedart:asyncsection of the library tour.