0

My app's backend server gives text response as stream.

Server (node.js): response.write(chunk);

I want to display the text immediately as the text is being received in chunks. Like this:

String text = '';
http.getStream(uri, (chunk) {
    setState() {
        text = text + chunk;
    }
});
  • You can use Stream.fromFuture to get the updated value, here the link: https://api.flutter.dev/flutter/dart-async/Stream/Stream.fromFuture.html – Wilson Toribio Aug 14 '23 at 12:32
  • @WilsonToribio Stream.fromFuture fires onData() only once. But I want chunks immediately as the server sends. – Sonu Mohammad Aug 14 '23 at 12:50
  • Check this answer: https://stackoverflow.com/questions/62433903/flutter-how-to-make-an-http-stream-for-streambuilder – Wilson Toribio Aug 14 '23 at 13:00
  • This answer is irrelevant. I want to show it like a ChatGPT response. Big text will be produced by the server and sent in chunks while producing. – Sonu Mohammad Aug 14 '23 at 13:21
  • If you are streaming data from the server it might be that you are using some sort of socket server, so there are packages in Flutter for socket listening for the specific socket server you are using. – Wilson Toribio Aug 14 '23 at 13:40

0 Answers0