我在我的项目中使用上传器。
存储库
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
final taskId = await uploader.enqueue(
url:
'https://xxxx',
files: files,
data: {
....
},
method: UploadMethod.POST,
showNotification: true,
tag: title);
final subscription = uploader.result.listen((result) async {
print("response " + result.response);
}, onError: (ex, stacktrace) {
print(ex.toString());
});
}
} catch (e) {
...
}
}当我第一次调用它时,uploader.result.listen只打印一次。但是如果我再次调用此方法,uploader.result.listen将调用两次。为什么?
编辑
我已将代码更改为
PageA
StreamSubscription<UploadTaskResponse> _subscription;
FlutterUploader uploader = FlutterUploader();
@override
void initState() {
super.initState();
_subscription = uploader.result.listen(
(result) {
// insert result to database
.....
},
onError: (ex, stacktrace) {
// ... code to handle error
},
);
}
void dispose() {
super.dispose();
_subscription.cancel();
_defectBloc.dispose();
}在A页,它有floatingActionButton。当单击浮动操作按钮时,它将打开B页。我将把uploader param传递给PageB和bloc,这样它就可以监听uploader。如果我在init页面上可以插入到本地数据库中的数据。如果我退出应用程序,我如何让插入也能工作呢?
发布于 2020-04-13 19:10:20
当您调用uploader.result.listen时,它每次都会添加一个订阅,如果您调用了n次,就会添加n个订阅。
要解决此问题,您需要使用取消()方法取消以前的订阅,或者只需添加一次订阅(在initState中,在dispose方法中为cancel )。
https://stackoverflow.com/questions/61194277
复制相似问题