Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Delays and Future.delayed | Futures and async/await
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Dart Asynchronous Flow

bookDelays and Future.delayed

When you need to introduce a pause or simulate a time-consuming operation in Dart asynchronous code, you can use Future.delayed. This constructor creates a Future that completes after a specified duration. It is commonly used to mimic network requests, database calls, or any operation where you want to wait before continuing your code.

The Future.delayed constructor takes two parameters:

  • The first parameter is a Duration object, which specifies how long the delay should be;
  • The second parameter is an optional callback function that returns the value with which the Future will complete.

By using await with Future.delayed, you can pause the execution of an async function for a given amount of time, making your code easier to read and control.

main.dart

main.dart

copy
123456789101112131415
import 'dart:async'; Future<String> fetchData() async { // Simulate a 2-second network delay return await Future.delayed( Duration(seconds: 2), () => 'Data loaded', ); } void main() async { print('Requesting data...'); String result = await fetchData(); print(result); }
question mark

Which statement best describes the behavior of Future.delayed in Dart?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you show an example of how to use `Future.delayed` in Dart?

What happens if I don't use `await` with `Future.delayed`?

Are there any best practices for using delays in asynchronous Dart code?

bookDelays and Future.delayed

Deslize para mostrar o menu

When you need to introduce a pause or simulate a time-consuming operation in Dart asynchronous code, you can use Future.delayed. This constructor creates a Future that completes after a specified duration. It is commonly used to mimic network requests, database calls, or any operation where you want to wait before continuing your code.

The Future.delayed constructor takes two parameters:

  • The first parameter is a Duration object, which specifies how long the delay should be;
  • The second parameter is an optional callback function that returns the value with which the Future will complete.

By using await with Future.delayed, you can pause the execution of an async function for a given amount of time, making your code easier to read and control.

main.dart

main.dart

copy
123456789101112131415
import 'dart:async'; Future<String> fetchData() async { // Simulate a 2-second network delay return await Future.delayed( Duration(seconds: 2), () => 'Data loaded', ); } void main() async { print('Requesting data...'); String result = await fetchData(); print(result); }
question mark

Which statement best describes the behavior of Future.delayed in Dart?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
some-alt