【发布时间】:2020-04-13 18:30:06
【问题描述】:
我在我的项目中使用https://pub.dev/packages/flutter_uploader。
存储库
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 会调用两次。为什么?
编辑
我已经把我的代码改成了这个
页面A
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参数传递给PageB和bloc,这样它就可以监听uploader。如果我在初始化页面上,数据能够插入到本地数据库中。如果我退出应用程序,如何让插入也起作用?
【问题讨论】: