Reputation: 1840
I using https://pub.dev/packages/flutter_uploader in my project.
repository
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) {
...
}
}
When first time I call this, uploader.result.listen
will only print once. But if I call this method again, uploader.result.listen
will call twice. Why?
Edit
I have changed my code to this
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();
}
In page A, it has floatingActionButton. When the floating action button is clicked, it will open page B. I will pass the uploader
param to PageB and bloc
, so it can listen to the uploader
. Data able to insert into local database if I on the init page. How can I let the insert work too if I exit the app?
Upvotes: 0
Views: 4563
Reputation: 107121
When you call uploader.result.listen
it'll add a subscription each time, if you call that n times, n subscription will be added.
To fix the issue, either you need to cancel previous subscription using cancel() method or you have to add the subscription only once (In your initState and cancel in your dispose method).
Upvotes: 2