Flutter/Dart - Open a view after a delay

To open a view after some delay you can use Future.delayed:

Future.delayed(const Duration(milliseconds: 500), () {
  setState(() {
    // Here you can write your code for open new view
  });
});

You can do it in 2 ways:

  • Using Timer class.

    import 'dart:async';
    
    // Go to Page2 after 5s.
    Timer(Duration(seconds: 5), () {
      Navigator.push(context, MaterialPageRoute(builder: (_) => Screen2()));
    });
    
  • Using Future.delayed class:

    // Go to Page2 after 5s.
    Future.delayed(Duration(seconds: 5), () {
      Navigator.push(context, MaterialPageRoute(builder: (_) => Screen2()));
    });
    

The benefit of using Timer over Future is its ability to cancel.