How to run code after some delay in Flutter?

Bradley Campbell picture Bradley Campbell · Mar 25, 2018 · Viewed 97.5k times · Source

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'd like to start with a default FlutterLogo Widget and then change its style property after some duration.

Answer

Rahul Sharma picture Rahul Sharma · Dec 14, 2018

You can use Future.delayed to run your code after some time. e.g.:

Future.delayed(const Duration(milliseconds: 500), () {

// Here you can write your code

  setState(() {
    // Here you can write your code for open new view
  });

});

In setState function, you can write a code which is related to app UI e.g. refresh screen data, change label text, etc.