Future (Flutter- Async Programming with Dart) — Part 3

Abdur Mohammed
3 min readAug 8, 2022

--

Part of Flutter/Dart Shorts: To the point| Simple.

1.0 Pre-requisites

  • Asynchronous coding experience in any language (C#, Java etc.)
  • Basic Dart at minimum.
  • Read previous articles on event loops and isolates to understand section 9.

2.0 Why is this important?

  • Future is a vital part of asynchronous programming in dart.

3.0 What is a Future?

  • In simple terms, it can be defined as a promise to return either value or error at some point ahead in time.
  • It is the result of an async function call or operation.
Future<T> where T is the type of result.Future<Person> getPerson(int id);

4.0 States of Future

A future can be ever be in two states:

4.1 Uncompleted

  • When an async function is called, it returns an uncompleted future. The caller can hold on to this future while the async operation completes.

4.2 Completed

Two possibilities:

  • Completed successfully with a value
  • Completed with an error
States of future

5.0 Then method

  • This is an instance method called when the future completes with a value.
asyncFunc().then(processResult);

6.0 Catch error method

  • Also an instance method which is called if the future completes with an error.
asyncFunc().then(processResult).catchError(reportError);

7.0 WhenComplete method

  • This is called when the future completes regardless of whether it returns data or an error.
asyncFunc().then(processResult).catchError(reportError).whenComplete(doSomethingRegardless);

8.0 Constructors

8.1 Value constructor

  • Simplest one. Generates a future with a completed value.
Example: Future<bool>.value(true)

8.2 Error constructor

  • Creates a future that will finish with an error
Example: Future.error(Exception(‘sample error’ ));

8.3 Delayed constructor

  • Takes two params.
  • Duration: To delay the execution.
  • Function: To execute after the duration.
Future.delayed(const duration(seconds: 3), (){
doSomething();
});

9.0 Event loops and Future

  • Future is an API created to make it convenient to use the event loops.
  • Dart executes most of the code on a single thread on a single event loop.
  • For example, if we make a remote request and it gives us a future back, event loop gets a future back which it keeps it on the side and deals with other tasks in the queue which may or may not be related or just hangs around.
  • When the future completes, the event queue executes the ‘then’ method or catch error method based on how it completes.

10.0 Future Builder

  • Widget that takes a future and a builder.
  • When the future completes, it rebuilds its children.
Example: FutureBuilder<String>( future: getRemoteData(), builder:( context, snapshot){})
  • Snapshot is the current state of the future. The widget can check its properties hasData and hasError to redraw itself accordingly.

That’s all for this week. Next week Streams!

You can find me on
LinkedInhttps://www.linkedin.com/in/knowabdur

Twitter: — https://twitter.com/AbdurDeveloper

References:

https://api.flutter.dev/flutter/dart-async/Future/Future.value.html

https://api.flutter.dev/flutter/dart-async/Future-class.html#constructors

https://api.flutter.dev/flutter/dart-async/Future/whenComplete.html

https://api.flutter.dev/flutter/dart-async/Future/Future.delayed.html

https://dart.dev/codelabs/async-await

https://www.youtube.com/watch?v=OTS-ap9_aXc

--

--

Abdur Mohammed

React Native| Flutter | Senior Software Engineer | Sydney, Australia