Your Second Flutter App

Nov 30 2021 · Dart 2.13, Flutter 2.2.3, Visual Studio Code

Part 1: Parse Network Data

03. Understand Futures

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 02. Create the Project Next episode: 04. Use a Model & Repository

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

A key aspect of app development is asynchronous programming. You want to keep your app user interface responsive, and asynchronous programming allows you to do that. There are a number of tasks that your app will need to do and that can take a non-trivial amount of time, like making calls to the network or writing a file to permanent storage. You need your app to continue running while those long running tasks happen. And how long they might take to finish is unknown when you start them. When they do finish, you need your app to process any results from the long running task, and then continue onward. Using such tasks is called asynchronous since the code does not run them in a synchronous manner, that most code runs. That is one line at a time and in order. You'll also hear the term concurrency used when describing such long running tasks, since they run concurrently with the rest of your app code. Now the dark data type Future, sometimes called a Promise in other languages is used when making such a synchronous calls. Futures let you run some asynchronous code and await is completion at some point in the Future, at which a value is returned to you. Futures make use of what is called the dark event loop. Dark code runs single threaded. The thread runs events from what is called the event queue. If you are coming from a language like JavaScript, you should be quite familiar with this approach. Things like user interactions with your app are examples of events. The completion of a Future is a another type of event that is handled by the event queue. When the Future completes, it's returned value is picked up in the event queue and the rest of the code then continues running. Now there are three possible states for Futures, incomplete, complete with success, and complete with error. The example shown here uses the getInstance method on a type called SharedPreferences. The getInstance method returns a Future of SharedPreference. Meaning, it runs a synchronously and returns a SharedPreference at some point in the Future. You use the then method on the Future type to determine what to do when the Future returns. Catcher will be called if there is an error during the processing and whenComplete is run, whether or not there is an error. Kind of like the finally block used with try-catch statements. Note that each of these methods returns the same Future instance, so they can be chained together like shown here. There is an alternative syntax that you can use with Futures and it utilizes the async and await dark keywords. Those keywords are also used in other languages like JavaScript, Python, in the latest version of Swift. The async/await syntax lets you write your code in a manner that looks a little like synchronous code. Part of the SharedPreference example from the last slide is replicated here using the async/await syntax. You label a method as async to indicate that it contains a long running task. Then you use await to signify that you want to wait for the result of the Future before continuing. With your new understanding of Futures, you can continue on building the RW courses app in the next episode.