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
Mastering DART
Mastering DART

Mastering DART: Master the art of programming high-performance applications with Dart

eBook
AU$41.99 AU$60.99
Paperback
AU$75.99
Subscription
Free Trial
Renews at AU$24.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Mastering DART

Chapter 2. Advanced Techniques and Reflection

In this chapter, we will discuss the flexibility and reusability of your code with the help of advanced techniques in Dart. Generic programming is widely useful and is about making your code type-unaware. Using types and generics makes your code safer and allows you to detect bugs early. The debate over errors versus exceptions splits developers into two sides. Which side to choose? It doesn't matter if you know the secret of using both. Annotation is another advanced technique used to decorate existing classes at runtime to change their behavior. Annotations can help reduce the amount of boilerplate code to write your applications. And last but not least, we will open Pandora's box through Mirrors of reflection. In this chapter, we will cover the following topics:

  • Generics
  • Errors versus exceptions
  • Annotations
  • Reflection

Generics

Dart originally came with generics—a facility of generic programming. We have to tell the static analyzer the permitted type of a collection so it can inform us at compile time if we insert a wrong type of object. As a result, programs become clearer and safer to use. We will discuss how to effectively use generics and minimize the complications associated with them.

Raw types

Dart supports arrays in the form of the List class. Let's say you use a list to store data. The data that you put in the list depends on the context of your code. The list may contain different types of data at the same time, as shown in the following code:

// List of data
List raw = [1, "Letter", {'test':'wrong'}];
// Ordinary item
double item = 1.23;

void main() {
  // Add the item to array
  raw.add(item);
  print(raw);
}

In the preceding code, we assigned data of different types to the raw list. When the code executes, we get the following result:

[1, Letter, {test...

Errors versus exceptions

Runtime faults can and do occur during the execution of a Dart program. We can split all faults into two types:

  • Errors
  • Exceptions

There is always some confusion on deciding when to use each kind of fault, but you will be given several general rules to make your life a bit easier. All your decisions will be based on the simple principle of recoverability. If your code generates a fault that can reasonably be recovered from, use exceptions. Conversely, if the code generates a fault that cannot be recovered from, or where continuing the execution would do more harm, use errors.

Let's take a look at each of them in detail.

Errors

An error occurs if your code has programming errors that should be fixed by the programmer. Let's take a look at the following main function:

main() {
  // Fixed length list
  List list = new List(5);
  // Fill list with values
  for (int i = 0; i < 10; i++) {
    list[i] = i;
  }
  print('Result is ${list}');
}

We created an...

Annotations

An annotation is metadata—data about data. An annotation is a way to keep additional information about the code in the code itself. An annotation can have parameter values to pass specific information about an annotated member. An annotation without parameters is called a marker annotation. The purpose of a marker annotation is just to mark the annotated member.

Dart annotations are constant expressions beginning with the @ character. We can apply annotations to all the members of the Dart language, excluding comments and annotations themselves. Annotations can be:

  • Interpreted statically by parsing the program and evaluating the constants via a suitable interpreter
  • Retrieved via reflection at runtime by a framework

Note

The documentation generator does not add annotations to the generated documentation pages automatically, so the information about annotations must be specified separately in comments.

Built-in annotations

There are several built-in annotations defined in the...

Reflection

Introspection is the ability of a program to discover and use its own structure. Reflection is the ability of a program to use introspection to examine and modify the structure and behavior of the program at runtime. You can use reflection to dynamically create an instance of a type or get the type from an existing object and invoke its methods or access its fields and properties. This makes your code more dynamic and can be written against known interfaces so that the actual classes can be instantiated using reflection. Another purpose of reflection is to create development and debugging tools, and it is also used for meta-programming.

There are two different approaches to implementing reflection:

  • The first approach is that the information about reflection is tightly integrated with the language and exists as part of the program's structure. Access to program-based reflection is available by a property or method.
  • The second approach is based on the separation of reflection...

Summary

This concludes mastering of the advanced techniques in Dart. You now know that generics produce safer and clearer code, annotation with reflection helps execute code dynamically, and errors and exceptions play an important role in finding bugs that are detected at runtime.

In the next chapter, we will talk about the creation of objects and how and when to create them using best practices from the programming world.

Left arrow icon Right arrow icon

Description

If you are an application developer who has experience with Dart and want to develop reusable and robust code in Dart, then this book is for you. You are expected to have a basic knowledge of core elements and applications.

What you will learn

  • Build applications easily using the eventdriven paradigm
  • Familiarize yourself with asynchronous programming
  • Understand when and how to use collections to store and manipulate groups of objects
  • Use Dart and JavaScript together to build web applications
  • Add internalization and localization support to your application to improve its performance
  • Organize clienttoserver communication and discover the protocols for specific scenarios
  • Detect and use HTML5 features that will help you deliver rich, crossplatform content
  • Discover different techniques to secure your web application from unauthorized users

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 20, 2014
Length: 346 pages
Edition : 1st
Language : English
ISBN-13 : 9781783989577
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Nov 20, 2014
Length: 346 pages
Edition : 1st
Language : English
ISBN-13 : 9781783989577
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
AU$24.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
AU$249.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick icon Exclusive print discounts
AU$349.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total AU$ 234.97
Learning Dart
AU$90.99
Mastering DART
AU$75.99
DART Cookbook
AU$67.99
Total AU$ 234.97 Stars icon
Banner background image

Table of Contents

13 Chapters
1. Beyond Dart's Basics Chevron down icon Chevron up icon
2. Advanced Techniques and Reflection Chevron down icon Chevron up icon
3. Object Creation Chevron down icon Chevron up icon
4. Asynchronous Programming Chevron down icon Chevron up icon
5. The Stream Framework Chevron down icon Chevron up icon
6. The Collection Framework Chevron down icon Chevron up icon
7. Dart and JavaScript Interoperation Chevron down icon Chevron up icon
8. Internalization and Localization Chevron down icon Chevron up icon
9. Client-to-server Communication Chevron down icon Chevron up icon
10. Advanced Storage Chevron down icon Chevron up icon
11. Supporting Other HTML5 Features Chevron down icon Chevron up icon
12. Security Aspects Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.6
(5 Ratings)
5 star 80%
4 star 0%
3 star 20%
2 star 0%
1 star 0%
Honest Dime Apr 08, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Mastering Dart by Sergey Akopkokhyants takes Dart to a new level. This book assumes you have application programming background like Dart, Java or C#.Highlights of the book.- talks about best practices. So gives rule of thumb approach.- explains design patterns like factory pattern, singleton pattern.- has conversational style of explanation that makes me feel the author takes us along.- focus on concurrency - which is a good thing. EDA, isolates, etc. This is an enterprise grade rich language.- concept of streams for pipelining tasks.- author shares some opinions like variables-or-accessor methods, inheritance-vs-composition.- Provides some useful references to profiling tools like wrk.Chapter wise core topicsChapter 1: Beyond Dart's Basics: Core topics: passing functions around, closures and mixins.Chapter 2: Advanced Techniques and Reflection Core topics: annotations, proxies and reflectionChapter 3: Object Creation Core topics: types of constructors- named constructor, redirecting constructor, private, factory constructors and initializing variables.Chapter 4: Asynchronous Programming Core topics: Dart VM execution model, call-vs-event stack architecture and differences between them, futures, zones(configurable execution context) and isolates.Chapter 5: The Stream Framework Core topics: streams - types and management, event sinks.Chapter 6: The Collection Framework Core topics: "usual" collection and more.Chapter 7: Dart and JavaScript Interoperation Core topics: Interaction with jQuery.Chapter 8: Internalization and Localization Core topics: "usual" stuff.Chapter 9: Client-to-server Communication Core topics: Handling HTTP request and response streams, AJAX (long)polling, server-sent events.Chapter 10: Advanced Storage Core topics: Cookies, Web Storage, Web SQL and IndexedDBChapter 11: Supporting Other HTML5 Features Core topics: Web Notification, drag-and-drop, geolocation APIs and CanvasChapter 12: Security Aspects Core topics: Web security, Securing a server, Securing a client and best practices.There is this sentence, "The website must have permission before use your location information." The author could have had the book proof read well. But as long as such sentence constructs do not have material ambiguity, I am willing to give the benefit of doubt.What I like most is the methodical flow of topics that builds on foundation and leaves us at the optimal level to explore things for ourselves based on the needs of the project.Disclosure: I got a copy to review and am not compensated for this review. My background is J2EE/backend stuff. For a person with purely UI background (HTML/CSS/Javascript), this may not be the best book to start with, IMHO.
Amazon Verified review Amazon
Jeffrey Johnson Jan 21, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Even though this book covers advanced topics, it doesn't storm ahead, leaving the reader trying to play catch-up. Examples and chapters build, in comfortable increments, on previous ones and there's hardly any "but more on that later" as I've seen in many other books.Sergey manages to stay focused on the advanced topics without falling into the trap of rehashing the basics of Dart. His writing is simple and to the point and I appreciate that.Topics are discussed in detail and I didn't feel that he'd glossed over any of them. I'm not a Dart developer myself, yet I was able to grasp the concepts fairly easily. There was hardly any of that "what did I just read" feeling I get with some books.(Disclosure: I know and work with the author)
Amazon Verified review Amazon
Dima Jan 21, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great book that delving deep into Dart. Must have for learning Dart, absolutely.
Amazon Verified review Amazon
Corneliu Dascalu Dec 27, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I really enjoyed it. I found it suitable for my level of experience with Dart (beginner). The author doesn't insist on basic stuff that can be easily found in the official docs, but explains in-depth the more advanced features of Dart.I feel that I have learned a lot, and that I can use the book as a resource for future projects.
Amazon Verified review Amazon
Paul Jan 19, 2015
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
There are not many Dart books published yet but there is some good information collected in this book. I disagree with some of the content, but chapters are a good length and have decent summaries.I would recommend it only for experienced developers that are healthily skeptical about things claimed to be “good practice” and “high performance” throughout the book.To be fair the blurb on the site also warns it requires some experience, but I surmise for different reasons. The book feels like it is imposing Java and enterprise ideals on Dart. Dart does not need all that baggage – even if the language itself enables it. To an impressionable mind this book could encourage some poor engineering choices.For example it uses object composition examples based on real life concepts – an inexperienced programmer might take that literally… modeling for real life rather than for how data is best represented and manipulated. There are no warnings about the cost of using reflection, how either type of optional parameter makes life harder for Dart and Javascript VMs, costs of object instantiation or the different types of function calls presented. There are many places throughout the book a novice could be mislead down a very poorly performing path.There are English language errors throughout. Catched rather than caught, angel rather than angle. There are a few phrases that sound conversational and a little out of place. It sometimes made reading a little jarring. I believe these issues could be solved with another editorial pass, but this version lacks polish.The book redeems itself back to three out of five when the reader has confidence to ignore the overtly enterprise style code. The language features and packages presented in later chapters were more useful.I have an e-copy of the book from Packt and read it entirely on a Kindle Paperwhite. The main text was typeset quite nicely and with the smallest font on my Kindle Paperwhite the code samples mostly fit on single lines.Disclosure: I was given a review copy and no compensation.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.