I\'d like to execute a function after a certain delay after my Widget is built. What\'s the idiomatic way of doing this in Flutter?
What I\'m trying to achieve: I\'
await Future.delayed(Duration(milliseconds: 1000));
Just leaving here the snippet everyone is looking for:
Future.delayed(Duration(milliseconds: 100), () {
// Do something
});
Figured it out
Trigger actions after countdown
Timer(Duration(seconds: 3), () {
print("Yeah, this line is printed after 3 seconds");
});
Repeat actions
Timer.periodic(Duration(seconds: 5), (timer) {
print(DateTime.now());
});
Trigger timer immediately
Timer(Duration(seconds: 0), () {
print("Yeah, this line is printed immediately");
});
import 'dart:async';
Timer timer;
void autoPress(){
timer = new Timer(const Duration(seconds:2),(){
print("This line will print after two seconds");
});
}
autoPress();
(Adding response on old q as this is the top result on google)
I tried yielding a new state in the callback within a bloc, and it didn't work. Tried with Timer and Future.delayed.
However, what did work was...
await Future.delayed(const Duration(milliseconds: 500));
yield newState;
Awaiting an empty future then running the function afterwards.