Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Cleanup with whenComplete | Chaining and Coordination
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Dart Asynchronous Flow

bookCleanup with whenComplete

When working with asynchronous operations in Dart, you often need to perform some cleanup or final actions after a Future completes. The whenComplete method provides a way to register such actions that will always run, whether the Future completes with a value or throws an error. This is different from then, which only runs if the Future completes successfully, and catchError, which only runs if there is an error.

Use whenComplete when you need to guarantee that some code runs after a Future ends, regardless of its outcome. This is especially useful for tasks like closing files, stopping timers, or printing status messages that should always happen after an operation.

main.dart

main.dart

copy
1234567891011121314151617181920212223
import 'dart:math'; Future<void> performOperation() async { print('Starting operation...'); // Randomly succeed or fail if (Random().nextBool()) { await Future.delayed(Duration(seconds: 1)); print('Operation succeeded.'); } else { await Future.delayed(Duration(seconds: 1)); throw Exception('Operation failed!'); } } void main() { performOperation() .whenComplete(() { print('Cleanup: Operation finished (success or error).'); }) .catchError((e) { print('Caught error: $e'); }); }
question mark

Which statement best describes the role of whenComplete in a Dart Future chain?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you give an example of how to use whenComplete in Dart?

What is the difference between whenComplete and finally in a try-catch block?

Are there any best practices for using whenComplete with asynchronous code?

bookCleanup with whenComplete

Desliza para mostrar el menú

When working with asynchronous operations in Dart, you often need to perform some cleanup or final actions after a Future completes. The whenComplete method provides a way to register such actions that will always run, whether the Future completes with a value or throws an error. This is different from then, which only runs if the Future completes successfully, and catchError, which only runs if there is an error.

Use whenComplete when you need to guarantee that some code runs after a Future ends, regardless of its outcome. This is especially useful for tasks like closing files, stopping timers, or printing status messages that should always happen after an operation.

main.dart

main.dart

copy
1234567891011121314151617181920212223
import 'dart:math'; Future<void> performOperation() async { print('Starting operation...'); // Randomly succeed or fail if (Random().nextBool()) { await Future.delayed(Duration(seconds: 1)); print('Operation succeeded.'); } else { await Future.delayed(Duration(seconds: 1)); throw Exception('Operation failed!'); } } void main() { performOperation() .whenComplete(() { print('Cleanup: Operation finished (success or error).'); }) .catchError((e) { print('Caught error: $e'); }); }
question mark

Which statement best describes the role of whenComplete in a Dart Future chain?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2
some-alt