Mobile Application Development
Duration: 65 min High School Grades: 9 - 12 CCSS, NGSS
Week 12
Chapter 7
The Future is Now: Introduction to Asynchronous
Programming
Book: Flutter Cookbook -
Over 100 proven techniques and solutions for app
development with Flutter 2.2 and Dart
Dr. Yasir Faheem
Associate Professor, NUST-SEECS
https://www.yasirfaheem.com/
Disclaimer
These lecture slides have been prepared using the content available on the official websites of
Dart, Flutter, and other online resources.
Primarily, this lecture has been prepared from:
Chapter 7 of the textbook titled, “Flutter Cookbook Over 100 proven techniques and solutions for
app development with Flutter 2.2 and Dart” by Harrington
Two video tutorials shared by the official channel of Flutter on Youtube:
https://www.youtube.com/watch?v=OTS-ap9_aXc
https://www.youtube.com/watch?v=SmTCmDMi4BY
All the copyrights are reserved with the original content creators.
The purpose of these slides is to only teach the concepts to students.
2
Outline
3
Asynchronous Programming - Future
Gift box
When closed => uncompleted
When opened i.e. completed , you get either
A value, or
An error
Order food from a restaurant
4
Asynchronous Programming - Future
5
Future
6
Future
7
How to get an Instance of a Future
Indirectly returns a future
8
Future - Methods
9
Future
10
then may return a Future of its Own
11
What if Future Completes with an Error?
then expects a value!
12
whenComplete() Method
Execute a task after a Future
has completed.
Irrespective of whether with
A value, or
An error.
13
FutureBuilder
14
FutureBuilder
Updating UI after retrieving data is
common
Use a FutureBuilder to integrate
Futures within a widget tree
Automatically updates its content when
the Future updates.
Advantage:
You can skip the setState instruction,
and Flutter will only rebuild the part of
the UI that needs updating.
Because FutureBuilder builds itself
based on the status of a Future
15
FutureBuilder
Handling when
Future completes
with an error
16
FutureBuilder
Handling when Future
completes with data.
17
FutureBuilder
Handling uncompleted
Future.
18
Asynchronous Programming with
Async and Await
19
Example
Asynchronous Operations:
Network fetch and file I/O
We want to write a single method that
put these pieces together.
How?
20
Option 1: Implementation using Future
We want to write a single method that put these pieces together.
21
Option 2: Async and Await
The good thing with Async and Await
Code looks like synchronous code, and
Uses Futures
22
Comparison: Futures API vs Async and Await
23
Comparison: Futures API vs Async and Await
24
Comparison: Futures API vs Async and Await
25
Error Handling with Async and Await
26
Let us See a Few Examples
Reference: Chapter 7 of the book
27
Using a Future
28
Using Async and Await to Remove Callbacks
Using Async and Await to Remove Callbacks
Resolving Errors in Asynchronous code
Using the FutureBuilder to let Flutter manage your Futures
Firing multiple futures at the same time.
Using Futures with Stateful widgets.
Turning Navigation Routes into asynchronous functions
29