Created
February 27, 2019 14:20
-
-
Save vovahost/debbde639489289f483be74ae1bdc1e4 to your computer and use it in GitHub Desktop.
CancelableOperation and CancelableCompleter usage example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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