Using a Future
When you write your code, you generally expect your instructions to run sequentially, one line after the other. For instance, let's say you write the following:
int x = 5;
int y = x * 2;
You expect the value of y
to be equal to 10 because the instruction int x = 5
completes before the next line. In other words, the second line waits for the first instruction to complete before being executed.
In most cases, this pattern works perfectly, but in some cases, and specifically, when you need to run instructions that take longer to complete, this is not the recommended approach, as your app would be unresponsive until the task is completed. That's why in almost all modern programming languages, including Dart, you can perform asynchronous operations.
Asynchronous operations do not stop the main line of execution, and therefore they allow the execution of other tasks before completing.
Consider the following diagram:
In the diagram, you can see how the main execution...