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

Arrow left icon
Profile Icon Akopkokhyants
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.6 (5 Ratings)
Paperback Nov 2014 346 pages 1st Edition
eBook
$22.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Akopkokhyants
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.6 (5 Ratings)
Paperback Nov 2014 346 pages 1st Edition
eBook
$22.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$22.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781783989560
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

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 included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.