I have a simple problem but I'm new to Flutter. I have to change the colors of 3 buttons, then wait 2 seconds and call another function. This is the Code:
setState((){
clr[0]= 0==gst ? Colors.green:Colors.red;
clr[1]= 1==gst ? Colors.green:Colors.red;
clr[2]= 2==gst ? Colors.green:Colors.red;
});
sleep(const Duration(seconds:2));
cardKey.currentState.toggleCard(); // second function
The problem is that this code waits 2 seconds then change the colors and call the second function...
I tried also with the sleep()
inside the setstate()
You should be using Future.delayed
to solve your issues as sleep
is not recommended. Make sure to define your function as async
. However, if you don't want to mark as async (or the function doesn't work with async), you can use this:
Future.delayed(Duration(seconds: 2)).then((_) {
cardKey.currentState.toggleCard(); // second function
});
For more reading, I recommend this.