skip to Main Content

I have a Flutter form where I want to auto run a process 2 seconds after a form is displayed.

How can I do this?

2

Answers


  1. Use Future.delayed function

    autoRunProcess(){
     return Future.delayed(const Duration(seconds: 2), () {
        setState(() {});
      });
    }
    

    You may also use Timer:

    autoRunProcess(){
     return Timer(Duration(seconds: 2), (){
       setState(() {});
     });
    }
    

    You may find other alternatives from this reference: How to run code after some delay in Flutter?

    Login or Signup to reply.
  2. void delayAction(){
      // Delay for 2 seconds
     Future.delayed(Duration(seconds: 2), () {
         // Call another function after the delay
         afterDelayCallFunction();
     });
    }
    

    Future.delayed in Dart/Flutter is a mechanism for introducing delays in asynchronous code execution. It allows you to schedule a function to be called in the future after a specified duration. This can be useful in scenarios where you want to wait for a certain period of time before executing a particular piece of code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search