Book Image

Learning Dart

Book Image

Learning Dart

Overview of this book

Table of Contents (19 chapters)
Learning Dart
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Asynchronous calls and Futures


How should our app handle a situation where it needs a service that can take some time to return its result, for example, when we have to fetch or store data, read a large file, or need a response from a web service. Obviously, the app can't wait for this to end and the result to arrive (the so called synchronous way), because this would freeze the screen (and the app) and frustrate users. A responsive web app must be able to call this service and, immediately, continue with what it was doing. Only when the result returns should it react and call some function on the response data. This is called working in an asynchronous, non-blocking way, and most of the methods in dart:io for server-side Dart apps work in this way. Developers with a Java or C# background would immediately think of starting another thread to do the additional work, but Dart can't do this; it has to compile to JavaScript, so Dart also works in a single-threaded model that is tightly controlled...