Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart Stream's listen() not calling onDone

Tags:

dart

dart-io

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?

like image 923
Nathaniel Johnson Avatar asked Jan 22 '14 18:01

Nathaniel Johnson


1 Answers

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));
like image 79
Dorrin-sot Avatar answered Oct 14 '22 02:10

Dorrin-sot