I want to cancel a function before it reaches the end if I call stop()
.
class Example {
late final completer = CancelableCompleter();
final List<String> list;
Example(this.list);
void start() {
completer.complete(doWork());
}
Future<void> doWork() async {
for(final e in list) {
await a();
await b();
}
await c();
print("end");
}
void stop() {
completer.operation.cancel();
}
}
I followed this is there any way to cancel a dart Future? but the execution stills happens.
Should I put a bunch of if(completer.isCancelled)
before every await
call that I make?