Search code examples
flutterdartsemaphore

Semaphore in flutter


Code:

void _test() {
  print(1);
  Timer.run(() {
    print(2);
  });
  print(3);
}

Print 1 3 2.

I want print 1 2 3.

In iOS I can use Semaphore, how can I do this in flutter?


Solution

  • Thanks @jamesdlin, I solved with his comment, below is desired code:

    void _test() async {
      print(1);
      Completer<void> completer = Completer();
      Timer.run(() {
        print(2);
        completer.complete();
      });
      await completer.future;
      print(3);
    }