Flutter/Dart – Open a view after a delay

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.

Leave a Comment