Santo Shakil
Santo Shakil

Reputation: 1052

How to execute a method after the execution of another method in Flutter(Dart)

I want to execute a method after the successful execution of another method. How can I do it? Please tell me with example.

Upvotes: 2

Views: 2585

Answers (1)

AskNilesh
AskNilesh

Reputation: 69681

You need to make your method Asynchronous

you can use futures, async, await

Try this way

Make your first method async method like below example

Future<int> methodOne() async {
    debugPrint('first method called');
    sleep(const Duration(seconds: 3));
    return 4;

  }
  methodTwo() {
    debugPrint('second method called');
  }

now you can call your methods like this

          GestureDetector(
              onTap: () async {
                await methodOne().then((value) {
                  methodTwo();
                });
              },
              child: Center(child: Padding(
                padding: const EdgeInsets.all(20.0),
                child: Text("Click Me"),
              ))), 

For more information please read Asynchronous programming in flutter

Upvotes: 4

Related Questions