How do I implement a timer to execute code after a certain delay?
Solution 1
Though overall use case is still not clear from your question/description, I think there is no need to use timer. Use delayed future instead, like this:
class _HomeScreenState extends State<HomeScreen> {
...
@override
void initState() {
super.initState();
Future.delayed(Duration(seconds: 2)).then((_) => action());
}
void action() {
print("executing action ...");
}
...
}
Solution 2
There are two ways to do that
- Timer
- Future
Timer
Timer(Duration(seconds: 2), () {
print("Execute this code afer 2 seconds");
});
Future:
Future.delayed(Duration(seconds: 2), () {
print("Execute this code afer 2 seconds");
});
So, What is the difference between the above two approaches?
With Timer
The timer
runs its job after the given duration, but flutter not waiting for it to complete its execution, it performs below statements.
Example:
Timer(Duration(seconds: 2), () {
print("Execute this code afer 2 seconds");
});
print("Other code");
Output:
Other code
Execute this code after 2 seconds
So as you can see code below timer will execute first and then the timer will be performed. Also, Timer can be stopped at any given point before its execution, if we crate the object of it.
Timer timer = Timer(Duration(seconds: 2), () {
print("Execute this code afer 2 seconds");
});
timer.cancel();
With Future
The future
also runs its job after the given duration, but its return future object means we can use await to get its execution first, and then below statements will be going to execute.
await Future.delayed(Duration(seconds: 2), () {
print("Execute this code afer 2 seconds");
});
print("My Code");
print("Other code");
Output:
Execute this code after 2 seconds
Other code
The main disadvantage of the future
is that we can't cancel it in between.
Admin
Updated on December 15, 2022Comments
-
Admin over 1 year
I am a new developer using Flutter. After reading Flutter docs and Stackoverflow, I cannot resolve my problem. I have trying to write code that displays a screen then waits 2 seconds before executing a print statement. What am I missing? Thanks in advance for your help.
import 'package:flutter/material.dart'; import 'dart:async'; class HomeScreen extends StatefulWidget { @override _HomeScreenState createState() => _HomeScreenState(); } class _HomeScreenState extends State<HomeScreen> { Timer timer; @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Color(0XFF5C894E), body: Column( children: [ Container( alignment: Alignment.center, child: Image( image: AssetImage('images/Mezzo_Welcome_Screen.png'), ), ), ], ), ); } void timeDelay() { timer = new Timer(const Duration(seconds: 2), () { print("This line will print after two seconds"); }); timeDelay(); timer.cancel(); } }
-
CEO tech4lifeapps about 3 yearsYou declare a function timeDelay() but you never actually call it. So this makes no sense.
-
Admin about 3 years@CEO tech4lifeapps My question has already been addressed. See previous comments.
-
-
Admin about 3 yearsBasically I just need to be able to have a delay before executing a new part of the code. Your solution works as well. Many thanks.
-
Alex Radzishevsky about 3 yearsIssue is that how do you know that 2 seconds is enough... What are you going to execute in action ?
-
Admin about 3 yearsThe app will open and load display the home screen for 2 seconds and then I will use Navigator.push to display a new route.
-
Alex Radzishevsky about 3 yearsOk, then I think it should work for you and if it does, you may accept it as correct solution
-
Alex Radzishevsky about 3 yearsThis is not correct answer, as it will perform action periodically every 2 seconds, while author wants to perform action only once
-
Alex Radzishevsky about 3 years@Manohara321 I am not sure that accepted answer is what you want
-
Admin about 3 years@ Alex Radzishevsky Updated..Getting used to StackOverflow.