Reputation: 4839
I have a stream with a transformer that fuses the UTF8.decoder
to the LineSplitter
. It works great but never calls the function specified in the onDone
parameter.
import 'dart:async';
import 'dart:io';
import 'dart:convert';
void main(List<String> arguments) {
Stream<List<int>> stream = new File("input.txt").openRead();
stream.transform(UTF8.decoder.fuse(const LineSplitter()))
.listen((line) {
stdout.writeln(line);
}, onDone: () {
stdout.write("done");
}).asFuture().catchError((_) => print(_));
}
Any ideas why it is never getting called?
Upvotes: 9
Views: 1432
Reputation: 331
The problem is that you used the asFuture()
method.
If you don't use that, onDone
will be called properly when EOF is reached; otherwise, you should put a .then((_) => print('done'))
after the Future
return value of asFuture()
for the same effect.
The resulting code should look like this:
(stream
.transform(utf8.decoder)
.transform(LineSplitter())
.listen((line) => print(line))
.asFuture())
.then((_) => print("done"))
.catchError((err) => stderr.writeln(err));
Upvotes: 1