Web Analytics Made Easy -
StatCounter
How-To-Implement-Flutter-Sleep-Functionality

Sometimes we may want to control our app execution to pause it for performing multiple tasks depending on our needs. In this article, we will see different ways to sleep our flutter app execution.

Timer Class

The Timer class can be used to run the code after a specific time because it has a duration property that can help us to sleep our app.

Example using Timer Class

Timer(Duration(seconds: 2), () {
// our code here
});

In the above example, the Timer constructor accepts two arguments the first one is duration which we set to 2 seconds and the second one is an anonymous function that can be used to execute our code. So after 2 seconds of sleep, our code will be executed.

Future.delayed

The Future class has a delayed method that can be used to sleep our app.

Example using Future.delayed

Future.delayed(Duration(seconds: 3), () {
// our code here
});

In the above example, the delayed method accepts two arguments the first one is duration which we set to 3 seconds and the
second one is the anonymous function which can be used to execute our code. So after 3 seconds of sleep, our code will be executed.

Sleep Method

The sleep method can also be used to pause our app execution.

Example using Sleep Method

Import the library

import 'dart:io';

Then we can use the sleep method

sleep(2000);

In the above example, the sleep method from the dart:io library accepts one argument which we passed as 2000. So after 2 seconds of sleep, our code will be executed.

Conclusion

We discussed the 3 ways to sleep in our app, you can use the one that best satisfies your need.

Click to rate this post!
[Total: 1 Average: 5]
Avatar

By AmmarJaved

Ammar Javed possesses a unique combination of skills and expertise in web, desktop, and mobile app development, and content writing. Ammar Javed is a strategic thinker and has problem-solving abilities. Ammar Javed can work independently and in a team. Ammar Javed is dedicated to continuous learning and professional development.

One thought on “How To Implement Flutter Sleep Functionality”

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »