Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart streams error with .listen().onError().onDone()

Tags:

dart

I have an issue with some code that looks like this. In this form I have an error

The expression here has a type of 'void', and therefore can't be used. Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void.dart(use_of_void_result).

If I remove the .onDone() the error goes away. Why? ELI5 please :-) I was looking at https://api.dart.dev/stable/2.7.0/dart-async/Stream/listen.html but seem to still be misundertanding something. I also read https://api.dart.dev/stable/2.7.0/dart-async/StreamSubscription/onDone.html

serviceName.UploadThing(uploadRequest).listen((response) {
  uploadMessageOutput = response.message;
  if (response.uploadResult) {
    showSuccess();
  } else {
    showError();
  }
  getUploadFileList(event);

  isSaveInProgress = false;
}).onError((error) {
  isSaveInProgress = false;
  _handleFileUploadError(uploadRequest, error);
}).onDone(() {
  isSaveInProgress = false;
});
like image 276
BenH Avatar asked Sep 02 '25 14:09

BenH


1 Answers

Your code is almost right, but will only require a simple change to work correctly.

You would be seeing the same error if you swapped the ordering of onError and onDone, so the issue has nothing to do with your stream usage. However, you're attempting to chain together calls to onError and then onDone which won't work since both of these methods return void.

What you're looking for is cascade notation (..), which will allow for you to chain calls to the StreamSubscription returned by listen(). This is what your code should look like:

serviceName.UploadThing(uploadRequest).listen((response) {
  uploadMessageOutput = response.message;
  if (response.uploadResult) {
    showSuccess();
  } else {
    showError();
  }
  getUploadFileList(event);

  isSaveInProgress = false;
})..onError((error) { // Cascade
  isSaveInProgress = false;
  _handleFileUploadError(uploadRequest, error);
})..onDone(() {       // Cascade
  isSaveInProgress = false;
});
like image 144
Ben Konyi Avatar answered Sep 05 '25 14:09

Ben Konyi