![]() A Future is something that in the future will give you a T.Ī future represents the result of an asynchronous operation, and can have two states: uncompleted or completed. So the promise to load a number from the database would return a Future while the promise to return a list of movies from an internet search might return a Future>. Since a method that takes some time cannot return immediately, it will return the promise of delivering a value when it's done. You can find the documentation for Dart here. It's not specific to flutter or dart, it exists under the same name in many other languages. This is achieved through a pattern known as async and await. Now how does the compiler know, whether it should actually stop everything and wait for a result or continue with all the background work and wait only in this instance? Well, it cannot figure that out on it's own. Operations that still run while your code waits for something. But how can an application run and show an animation and yet still wait for the result? The answer is: asynchronous operations. A spinner, an hourglass, maybe a progress bar. Even if something takes time, we get a little animation. ![]() You may remember this as standard if you are old enough, but in today's world that would be seen as a bug. ![]() When this flow is blocked, for example by waiting for a computation or resource access that takes time, the application just freezes. Most applications by default have a single flow of control. Imagine an avatar picture being loaded from a web server, a user's data being loaded from a database or just the texts of the app in multiple languages being loaded from device memory. Some functions, especially when they access resources like hardware or network, take a little time to do so. The function is called, runs and returns it's result. What is a Future and how do I use it?Īn object representing a delayed computation. In case you are familiar with Task or Promise and the async/ await pattern, then you can skip right to the "How to use a Future with the widgets in Flutter" section.
0 Comments
Leave a Reply. |