What is the difference in Future consructors?

Viewed 36

What is the difference in these Future consructors?

It seems they do and act the same way and the results are identical, arn't they?

Future<double> future1 = Future(() => 36.6);

Future<double> future2 = Future.value(36.6);
1 Answers

Future(callback) gives the DOM time to react and doesn't go into the runAsync queue.

Future.value(value) behaves like a Future that was completed earlier (it's completely valid to keep these Future.values around instead of creating a new one every time) and it does go into the runAsync queue.

Have a look on the example below (it returns 1,2,4,3 instead of 1,2,3,4):

    void main() {
      Future.value().then((_) {
        print(1);
        Future(() => print(3));
      });

      Future.value().then((_) {
        print(2);
        Future.value().then((_) => print(4));
      });
    }
Related