Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
DART Cookbook

You're reading from   DART Cookbook Over 110 incredibly effective, useful, and hands-on recipes to design Dart web client and server applications

Arrow left icon
Product type Paperback
Published in Oct 2014
Publisher Packt
ISBN-13 9781783989621
Length 346 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Ivo Balbaert Ivo Balbaert
Author Profile Icon Ivo Balbaert
Ivo Balbaert
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Working with Dart Tools FREE CHAPTER 2. Structuring, Testing, and Deploying an Application 3. Working with Data Types 4. Object Orientation 5. Handling Web Applications 6. Working with Files and Streams 7. Working with Web Servers 8. Working with Futures, Tasks, and Isolates 9. Working with Databases 10. Polymer Dart Recipes 11. Working with Angular Dart Index

Setting up the checked and production modes

When developing or maintaining, an app's execution speed is not so important, but information about the program's execution is. On the other hand, when the app is put in a customer environment to run, the requirements are nearly the opposite; speed is of utmost importance, and the less information the program reveals about itself, the better. That's why when an app runs in the Dart Virtual Machine (VM), it can do so in two runtime modes:

  • The Checked mode: This is also known as the debug mode. The checked mode is used during development and gives you warnings and errors of possible bugs in the code.
  • The Production mode: This is also known as the release mode. You deploy an app in the production mode when you want it to run as fast as possible, unhindered by code checks.

Getting ready

Open your app in Dart Editor and select the startup web page or Dart script, usually web\index.html.

How to do it...

  1. When working in Dart Editor, the checked mode is the default mode. If you want the production mode, open the Run menu and select Manage Launches (Ctrl + Shift + M). The Manage Launches window appears, as shown in the following screenshot:
    How to do it...

    The Manage Launches window

  2. Under Dartium settings, you will see the checkbox Run in checked mode. (If you have selected a Dart script, it will be under the header VM settings.) Uncheck this to run the script in the production mode. Next, click on Apply and then on Close, or on Run immediately. This setting will remain in place until you change it again.

Scripts that are started on the command line (or in a batch file) with the dart command run in the Dart VM and thus in the production mode. If you want to run the Dart VM in the checked mode, you have to explicitly state that with the following command:

dart –c script.dart or: dart --checked script.dart 

You can start Dartium (this is Chromium with the Dart VM) directly by launching the Chrome executable from dart\chromium; by default, it runs Dart Editor in the production mode. If you would like to start Dartium in the checked mode, you can do this as follows:

  • On Windows, in the dart\chromium folder, click on the chrome file
  • On Linux, in the ~/dart/chromium folder, open the ./chrome file
  • On OS X, open the DART_FLAGS folder and then open path/Chromium.app

Verify this setting by going to the following address in the Chrome browser that you just started chromium://version.

When a web app runs in the Dart VM in Chrome, it will run in the production mode, by default.

How it works...

In the checked mode, types are checked by calling assertions of the form assert (var1 is T) to make sure that var1 is of type T. This happens whenever you perform assignments, pass parameters to a function, or return results from a function.

However, Dart is a dynamic language where types are optional. That's why the VM must, in the production mode, execute your code as if the type annotations (such as int n) do not exist; they are effectively thrown away. So at runtime, the following statement int x = 1 is equivalent to var x = 1.

A binding x is created but the type annotation is not used.

Note

Avoiding type checks makes the production mode a lot faster. Also, the VM uses the type inference to produce faster code; it observes the type of the value (here, 1) assigned to x and optimizes accordingly.

There's more...

With the checked mode, Dart helps you catch type errors during development. This is in contrast to the other dynamic languages, such as Python, Ruby, and JavaScript, where these are only caught during testing, or much worse, they provoke runtime exceptions. You can easily check whether your Dart app runs in the checked mode or not by calling the function isCheckedMode() from main() (see the script test_checked_mode\bin\ test_checked_mode.dart in the Chapter 1 folder of the code bundle), as shown in the following code:

main() {
  isCheckedMode();
  // your code starts here  
}

void isCheckedMode() {
    try {
        int n = '';
        throw new Exception("Checked Mode is disabled!");
    } on TypeError {
      print("Checked Mode is enabled!");
    }
}

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

The exception message will be shown in the browser console. Be sure to remove this call or comment it out before deploying it to the production mode; we don't want an exception at runtime!

See also

  • The Compiling your app to JavaScript recipe of this chapter for how to enable the checked mode in the JavaScript version of the app
  • The Using the command-line tools recipe of this chapter for other options
You have been reading a chapter from
DART Cookbook
Published in: Oct 2014
Publisher: Packt
ISBN-13: 9781783989621
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image