Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Exiting from an app


A Dart program starts its execution from the main() function in one thread (or isolate) in the Dart VM. The Dart VM by design always starts up single threaded. The program can end in three different ways:

  • It can end in a normal way by executing the last statement from main() and returning the exit code with the value 0, which means success

  • It can terminate abnormally with a runtime exception, returning exit code different from 0, such as 255 in the case of an unhandled exception

  • It can wait in an event loop for user interaction (such as in the browser or a web server waiting for requests), and then terminate when the browser is closed or another app is started in the same browser tab

However, how can we exit the app from the code itself? This can be useful, for example, in a server-side VM app with some Futures that may or may not return.

How to do it...

The first possibility is to use the exit(int code) top-level function from dart:io, as in exit_app.dart, to stop the app...