0

How can I return a value from a download function that looks something like this:

Either<sucess, failure> download() {

   Http.streamedResponse response;

   response.stream.listen((chunk) {
     sink.add(chunk);
   }
} 

// on error, return failure
// on done, return success
May
  • 93
  • 7
  • 1
    `final result = await response.stream.pipe(sink)` – pskink Nov 24 '22 at 15:51
  • Hello, thank you!:) I don't understand. Pipe will only close the stream? I want for the download function to return just one value. Either myObject or failure. Before that, I need to listen to the stream continually (I need ongoing results and write/append the data into the file). – May Nov 24 '22 at 16:49
  • 1
    `Stream.pipe` will download all the data and its returned `Future` completes when the download is done – pskink Nov 24 '22 at 16:58
  • Yes, but I need to do something while the events are happening AND final value (that is unrelated to the events). – May Nov 24 '22 at 17:20
  • 1
    so use `Stream.map` method with the callback that do something with the input data and returns the same data, but I'm not sure what you mean by "final value", is it your download file? – pskink Nov 24 '22 at 17:38
  • No, the final data is just some custom object, unrelated to the stream (that needs to be returned from the download function once the stream completes). This final data needs to be returned by the download function. While the stream is streaming, I just need to save the chunks into the file (not returning any value). – May Nov 24 '22 at 17:41
  • I have no problems with saving the data. I just want to find a way of executing a function once the stream completes. You can only return void in 'onDone' that a stream has and that's my problem. I don't want to return void, but another value. – May Nov 24 '22 at 19:22
  • But I'll tylry with await for, thank you. Does this works the same as listening to the stream? – May Nov 24 '22 at 19:26
  • Can you catch errors in await for? – May Nov 24 '22 at 19:31
  • 1
    yes, like normal try... catch... - doesn't it work? – pskink Nov 24 '22 at 19:48
  • Thaaaanks A LOT, I see that it works normally with errors, you saved me again! I wish you a nice day! You can publish your answer so that I can approve it. – May Nov 24 '22 at 20:05
  • 1
    your welcome, feel free to write a self answer then – pskink Nov 24 '22 at 20:13

0 Answers0