我对一些看起来像这样的代码有一个问题。在此表单中,我有一个错误
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)。
如果我删除.onDone(),错误就会消失。为什么?请使用ELI5 :-)我一直在看https://api.dart.dev/stable/2.7.0/dart-async/Stream/listen.html,但似乎还是误解了一些东西。我还阅读了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;
});发布于 2020-01-18 05:21:25
您的代码几乎是正确的,但只需要简单的更改即可正常工作。
如果您交换了onError和onDone的顺序,您将看到相同的错误,因此该问题与您的流使用无关。但是,您试图将对onError的调用和对onDone的调用链接在一起,这将不起作用,因为这两个方法都返回void。
您正在寻找的是cascade notation (..),它允许您将调用链接到listen()返回的StreamSubscription。下面是你的代码应该是什么样子:
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;
});https://stackoverflow.com/questions/59794932
复制相似问题