In a Dart UI, I have a button [submit] to launch a long async request. The [submit] handler returns a Future. Next, the button [submit] is replaced by a button [cancel] to allow the cancellation of the whole operation. In the [cancel] handler, I would like to cancel the long operation. How can I cancel the Future returned by the submit handler? I found no method to do that.
相关问题
- What means in Dart static type and why it differs
- Flutter : Prepare list data from http request
- How to schedule an alarm on specific time in Flutt
- MappedListIterable is not a SubType
- What is the difference between generics and dynami
相关文章
- Observatory server failed to start - Fails to crea
- Adding Shadows at the bottom of a container in flu
- Flutter. Check if a file exists before loading it
- Receive share file intents with Flutter
- Do stateless widgets dispose on their own?
- how to implement Alphabet scroll in flutter
- Flutter: Is it possible to format (bold, italicize
- Make Function parameter optional in custom widget
As far as I know, there isn't a way to cancel a Future. But there is a way to cancel a Stream subscription, and maybe that can help you.
Calling
onSubmit
on a button returns aStreamSubscription
object. You can explicitly store that object and then callcancel()
on it to cancel the stream subscription:Later, as a response to some user action, perhaps, you can cancel the subscription:
Change the future's task from 'do something' to 'do something unless it has been cancelled'. An obvious way to implement this would be to set a boolean flag and check it in the future's closure before embarking on processing, and perhaps at several points during the processing.
Also, this seems to be a bit of a hack, but setting the future's timeout to zero would appear to effectively cancel the future.
One way I accomplished to 'cancel' a scheduled execution was using a
Timer
. In this case I was actually postponing it. :)The
runMultipleTimes()
method will be called multiple times in sequence, but only after 1 second of a batch theonceAtTheEndOfTheBatch
will be executed.