Skip to content

Instantly share code, notes, and snippets.

@vovahost
Created February 27, 2019 14:20
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vovahost/debbde639489289f483be74ae1bdc1e4 to your computer and use it in GitHub Desktop.
Save vovahost/debbde639489289f483be74ae1bdc1e4 to your computer and use it in GitHub Desktop.
CancelableOperation and CancelableCompleter usage example
import 'package:async/async.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test("CancelableOperation with future", () async {
var cancellableOperation = CancelableOperation.fromFuture(
Future.value('future result'),
onCancel: () => {print('onCancel')},
);
// cancellableOperation.cancel(); // comment/uncomment this to log the different callbacks
cancellableOperation.value.then((value) => {
print('then: $value'),
});
cancellableOperation.value.whenComplete(() => {
print('onDone'),
});
});
test("CancelableOperation with stream", () async {
var cancellableOperation = CancelableOperation.fromFuture(
Future.value('future result'),
onCancel: () => {print('onCancel')},
);
// cancellableOperation.cancel(); // comment/uncomment this to log the different callbacks
cancellableOperation.asStream().listen(
(value) => {
print('value: $value'),
},
onDone: () => {
print('onDone'),
},
);
});
test("CancelableCompleter is cancelled", () async {
CancelableCompleter completer = CancelableCompleter(onCancel: () {
print('onCancel');
});
// completer.operation.cancel(); // comment/uncomment this to log the different callbacks
completer.complete(Future.value('future result'));
print('isCanceled: ${completer.isCanceled}');
print('isCompleted: ${completer.isCompleted}');
completer.operation.value.then((value) => {
print('then: $value'),
});
completer.operation.value.whenComplete(() => {
print('onDone'),
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment