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
$22.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to Russia

Economy delivery 10 - 13 business days

$6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

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 : 9781783989560
Category :
Languages :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Russia

Economy delivery 10 - 13 business days

$6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.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
$199.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 $5 each
Feature tick icon Exclusive print discounts
$279.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 $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 169.97
Learning Dart
$65.99
Mastering DART
$54.99
DART Cookbook
$48.99
Total $ 169.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

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact [email protected] with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at [email protected] using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on [email protected] with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on [email protected] within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on [email protected] who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on [email protected] within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela