close

How to run code after some delay in Flutter?

Hello Guys How are you all? Hope You all are fine. Here we will talk about How to run code after some delay in a flutter. So today Here I come with all possible solutions for this error.

We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.

How to run code after some delay in Flutter?

  1. How to run code after some delay in Flutter?

    To run code after some delay in Flutter You can do it in two ways 1 is Future.delayed and 2 is Timer. Here we can use Future.delayed to run your code after some time. e.g.:
    Future.delayed(const Duration(milliseconds: 500), () {
    // Here Is Our Code
    });

  2. run code after some delay in Flutter

    To run code after some delay in Flutter You can do it in two ways 1 is Future.delayed and 2 is Timer. Here we can use Future.delayed to run your code after some time. e.g.:
    Future.delayed(const Duration(milliseconds: 500), () {
    // Here Is Our Code
    });

Method 1: Using Future.delayed

You can do it in two ways 1 is Future.delayed and 2 is Timer. Here we can use Future.delayed to run your code after some time. e.g.:

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

// Here Is Our Code

});

Just leaving here the snippet everyone is looking for:

Future.delayed(Duration(milliseconds: 100), () {
  // Do something
});

Solution 2: using Timer

Trigger actions after countdown

Timer(Duration(seconds: 3), () {
  print("Yeah, this line is printed after 3 seconds");
});

Repeat actions

Timer.periodic(Duration(seconds: 5), (timer) {
  print(DateTime.now());
});

Trigger timer immediately

Timer(Duration(seconds: 0), () {
  print("Yeah, this line is printed immediately");
});

Summery

So, It’s All About This tutorial. I hope this tutorial helps you to Solve your error. Please Comment Below if You stucks anywhere with my code. And please comment below on which solution worked for you. Thank You.

Also Check Out Below Tutorials

1 thought on “How to run code after some delay in Flutter?”

Leave a Comment

%d bloggers like this: