I would like to have simple API in my http server so everytime I write to HttpResponse I use stream.
so I convert all object into stream, ie object->json->stream
Stream<List<int>> toStream(Object value) {
var json = JSON.encode(value);
var controller = new StreamController<List<int>>(onListen: () => UTF8.encode(json));
return controller.stream;
}
and later
(response as HttpResponse).addStream(toStream({"a": 1, "B": 2})
.then(() => response.flush())
.catchError((e, stack) {
_logger.error("Handling ${context.path} finished with an error: $e");
_logger.debug(stack.toString());
})
.whenComplete(() => response.close());
but I get error
Uncaught Error: Bad state: StreamSink is bound to a stream
Stack Trace:
#0 _StreamSinkImpl.close (io_sink.dart:122)
#1 _HttpOutboundMessage.close (http_impl.dart:481)
Im not sure what I am doing wrong here. I saw examples where File's input stream was piped to response but I cannot make it work either.
any help appreciated!