| OLD | NEW | 
|     1 --- |     1 --- | 
|     2 # WARNING: GENERATED FILE. DO NOT EDIT. |     2 # WARNING: GENERATED FILE. DO NOT EDIT. | 
|     3 # WANT TO CONTRIBUTE? SEE https://github.com/dart-lang/dart-up-and-running-book |     3 # WANT TO CONTRIBUTE? SEE https://github.com/dart-lang/dart-up-and-running-book | 
|     4 layout: book |     4 layout: book | 
|     5 title: "Chapter 3. A Tour of the Dart Libraries from Dart: Up and Running" |     5 title: "Chapter 3. A Tour of the Dart Libraries from Dart: Up and Running" | 
|     6 description: "Read Chapter 3. A Tour of the Dart Libraries of Dart: Up and Runni
      ng, published by O'Reilly." |     6 description: "Read Chapter 3. A Tour of the Dart Libraries of Dart: Up and Runni
      ng, published by O'Reilly." | 
|     7 --- |     7 --- | 
|     8 <div class="navheader"><table width="100%" summary="Navigation header"><tr><th c
      olspan="3" align="center">Chapter 3. A Tour of the Dart Libraries</th></tr><tr><
      td width="20%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><th 
      width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n"
       href="ch04.html">Next</a></td></tr></table><hr></div><div class="chapter"><div 
      class="titlepage"><div><div><h1 class="title"><a name="ch03"></a>Chapter 3. A To
      ur of the Dart Libraries</h1></div></div></div><div class="toc"><p><b>Table of C
      ontents</b></p><dl class="toc"><dt><span class="sect1"><a href="ch03.html#ch03-d
      artcore---strings-collections-and-more">dart:core - Numbers, Collections, String
      s, and More</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch
      03-numbers">Numbers</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch
      03-strings-and-regular-expressions">Strings and Regular Expressions</a></span></
      dt><dt><span class="sect2"><a href="ch03.html#ch03-collections">Collections</a><
      /span></dt><dt><span class="sect2"><a href="ch03.html#ch03-uri">URIs</a></span><
      /dt><dt><span class="sect2"><a href="ch03.html#ch03-dates-and-times">Dates and T
      imes</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-utility-inte
      rfaces">Utility Classes</a></span></dt><dt><span class="sect2"><a href="ch03.htm
      l#ch03-exceptions">Exceptions</a></span></dt></dl></dd><dt><span class="sect1"><
      a href="ch03.html#ch03-asynchronous-programming">dart:async - Asynchronous Progr
      amming</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-fu
      tures">Future</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-str
      eams">Stream</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-more
      -information-9">More Information</a></span></dt></dl></dd><dt><span class="sect1
      "><a href="ch03.html#ch03-dart-math">dart:math - Math and Random</a></span></dt>
      <dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-trigonometry">Trigonomet
      ry</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-maximum-and-mi
      nimum">Maximum and Minimum</a></span></dt><dt><span class="sect2"><a href="ch03.
      html#ch03-math-constants">Math Constants</a></span></dt><dt><span class="sect2">
      <a href="ch03.html#ch03-random-numbers">Random Numbers</a></span></dt><dt><span 
      class="sect2"><a href="ch03.html#ch03-more-information-7">More Information</a></
      span></dt></dl></dd><dt><span class="sect1"><a href="ch03.html#ch03-dart-html-us
      ing-html5-apis">dart:html - Browser-Based Apps</a></span></dt><dd><dl><dt><span 
      class="sect2"><a href="ch03.html#ch03-manipulating-the-dom">Manipulating the DOM
      </a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-httprequest">Usi
      ng HTTP Resources with HttpRequest</a></span></dt><dt><span class="sect2"><a hre
      f="ch03.html#ch03-websockets">Sending and Receiving Real-Time Data with WebSocke
      ts</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-more-informati
      on-html">More information</a></span></dt></dl></dd><dt><span class="sect1"><a hr
      ef="ch03.html#ch03-dartio---file-and-socket-io-for-command-line-apps">dart:io - 
      I/O for Command-Line Apps</a></span></dt><dd><dl><dt><span class="sect2"><a href
      ="ch03.html#ch03-files-and-directories">Files and Directories</a></span></dt><dt
      ><span class="sect2"><a href="ch03.html#ch03-io-http">HTTP Clients and Servers</
      a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-more-information-i
      o">More Information</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch
      03.html#ch03-dart-convert">dart:convert - Decoding and Encoding JSON, UTF-8, and
       more</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-jso
      n">Decoding and Encoding JSON</a></span></dt><dt><span class="sect2"><a href="ch
      03.html#ch03-utf">Decoding and Encoding UTF-8 Characters</a></span></dt><dt><spa
      n class="sect2"><a href="ch03.html#ch03-utf-other">Other Functionality</a></span
      ></dt></dl></dd><dt><span class="sect1"><a href="ch03.html#ch03-mirrors">dart:mi
      rrors - Reflection</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.
      html#ch03-mirrors-symbols">Symbols</a></span></dt><dt><span class="sect2"><a hre
      f="ch03.html#ch03-mirrors-introspection">Introspection</a></span></dt><dt><span 
      class="sect2"><a href="ch03.html#ch03-mirrors-invocation">Invocation</a></span><
      /dt><dt><span class="sect2"><a href="ch03.html#ch03-more-information-mirrors">Mo
      re Information</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch03.ht
      ml#ch03-summary">Summary</a></span></dt></dl></div><p>This chapter shows you how
       to use the major features in Dart’s |     8 <div class="navheader"><table width="100%" summary="Navigation header"><tr><th c
      olspan="3" align="center">Chapter 3. A Tour of the Dart Libraries</th></tr><tr><
      td width="20%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><th 
      width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n"
       href="ch04.html">Next</a></td></tr></table><hr></div><div class="chapter"><div 
      class="titlepage"><div><div><h1 class="title"><a name="ch03"></a>Chapter 3. A To
      ur of the Dart Libraries</h1></div></div></div><div class="toc"><p><b>Table of C
      ontents</b></p><dl class="toc"><dt><span class="sect1"><a href="ch03.html#ch03-d
      artcore---strings-collections-and-more">dart:core - Numbers, Collections, String
      s, and More</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch
      03-numbers">Numbers</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch
      03-strings-and-regular-expressions">Strings and Regular Expressions</a></span></
      dt><dt><span class="sect2"><a href="ch03.html#ch03-collections">Collections</a><
      /span></dt><dt><span class="sect2"><a href="ch03.html#ch03-uri">URIs</a></span><
      /dt><dt><span class="sect2"><a href="ch03.html#ch03-dates-and-times">Dates and T
      imes</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-utility-inte
      rfaces">Utility Classes</a></span></dt><dt><span class="sect2"><a href="ch03.htm
      l#ch03-exceptions">Exceptions</a></span></dt></dl></dd><dt><span class="sect1"><
      a href="ch03.html#ch03-asynchronous-programming">dart:async - Asynchronous Progr
      amming</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-fu
      tures">Future</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-str
      eams">Stream</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-more
      -information-9">More Information</a></span></dt></dl></dd><dt><span class="sect1
      "><a href="ch03.html#ch03-dart-math">dart:math - Math and Random</a></span></dt>
      <dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-trigonometry">Trigonomet
      ry</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-maximum-and-mi
      nimum">Maximum and Minimum</a></span></dt><dt><span class="sect2"><a href="ch03.
      html#ch03-math-constants">Math Constants</a></span></dt><dt><span class="sect2">
      <a href="ch03.html#ch03-random-numbers">Random Numbers</a></span></dt><dt><span 
      class="sect2"><a href="ch03.html#ch03-more-information-7">More Information</a></
      span></dt></dl></dd><dt><span class="sect1"><a href="ch03.html#ch03-dart-html-us
      ing-html5-apis">dart:html - Browser-Based Apps</a></span></dt><dd><dl><dt><span 
      class="sect2"><a href="ch03.html#ch03-manipulating-the-dom">Manipulating the DOM
      </a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-httprequest">Usi
      ng HTTP Resources with HttpRequest</a></span></dt><dt><span class="sect2"><a hre
      f="ch03.html#ch03-websockets">Sending and Receiving Real-Time Data with WebSocke
      ts</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-more-informati
      on-html">More information</a></span></dt></dl></dd><dt><span class="sect1"><a hr
      ef="ch03.html#ch03-dartio---file-and-socket-io-for-command-line-apps">dart:io - 
      I/O for Command-Line Apps</a></span></dt><dd><dl><dt><span class="sect2"><a href
      ="ch03.html#ch03-files-and-directories">Files and Directories</a></span></dt><dt
      ><span class="sect2"><a href="ch03.html#ch03-io-http">HTTP Clients and Servers</
      a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-more-information-i
      o">More Information</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch
      03.html#ch03-dart-convert">dart:convert - Decoding and Encoding JSON, UTF-8, and
       more</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-jso
      n">Decoding and Encoding JSON</a></span></dt><dt><span class="sect2"><a href="ch
      03.html#ch03-utf">Decoding and Encoding UTF-8 Characters</a></span></dt><dt><spa
      n class="sect2"><a href="ch03.html#ch03-utf-other">Other Functionality</a></span
      ></dt></dl></dd><dt><span class="sect1"><a href="ch03.html#ch03-mirrors">dart:mi
      rrors - Reflection</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.
      html#ch03-mirrors-symbols">Symbols</a></span></dt><dt><span class="sect2"><a hre
      f="ch03.html#ch03-mirrors-introspection">Introspection</a></span></dt><dt><span 
      class="sect2"><a href="ch03.html#ch03-mirrors-invocation">Invocation</a></span><
      /dt><dt><span class="sect2"><a href="ch03.html#ch03-more-information-mirrors">Mo
      re Information</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch03.ht
      ml#ch03-summary">Summary</a></span></dt></dl></div><p>This chapter shows you how
       to use the major features in Dart’s | 
|     9   libraries. It’s just an overview, and by no means comprehensive. Whenever |     9   libraries. It’s just an overview, and by no means comprehensive. Whenever | 
|    10   you need more details about a class, consult the <a class="ulink" href="http:/
      /api.dartlang.org/" target="_top">Dart API reference.</a></p><div class="sect1">
      <div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="
      ch03-dartcore---strings-collections-and-more"></a>dart:core - Numbers, Collectio
      ns, Strings, and More</h2></div></div></div><p>The Dart core library provides a 
      small but critical set of built-in |    10   you need more details about a class, consult the <a class="ulink" href="http:/
      /api.dartlang.org/" target="_top">Dart API reference.</a></p><div class="sect1">
      <div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="
      ch03-dartcore---strings-collections-and-more"></a>dart:core - Numbers, Collectio
      ns, Strings, and More</h2></div></div></div><p>The Dart core library provides a 
      small but critical set of built-in | 
| (...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   432   assert(uri.scheme   == 'http'); |   432   assert(uri.scheme   == 'http'); | 
|   433   assert(uri.host     == 'example.org'); |   433   assert(uri.host     == 'example.org'); | 
|   434   assert(uri.path     == '/foo/bar'); |   434   assert(uri.path     == '/foo/bar'); | 
|   435   assert(uri.fragment == 'frag'); |   435   assert(uri.fragment == 'frag'); | 
|   436   assert(uri.origin   == 'http://example.org:8080'); |   436   assert(uri.origin   == 'http://example.org:8080'); | 
|   437 }</pre><p>See the <a class="ulink" href="http://api.dartlang.org/dart_core/Uri.h
      tml" target="_top">Uri API docs</a> |   437 }</pre><p>See the <a class="ulink" href="http://api.dartlang.org/dart_core/Uri.h
      tml" target="_top">Uri API docs</a> | 
|   438         for more URI components that you can get.</p></div><div class="sect3"><d
      iv class="titlepage"><div><div><h4 class="title"><a name="ch03-uri-building"></a
      >Building URIs</h4></div></div></div><p>You can build up a URI from individual p
      arts using the |   438         for more URI components that you can get.</p></div><div class="sect3"><d
      iv class="titlepage"><div><div><h4 class="title"><a name="ch03-uri-building"></a
      >Building URIs</h4></div></div></div><p>You can build up a URI from individual p
      arts using the | 
|   439         <code class="literal">Uri()</code> <span class="keep-together">construct
      or</span>:</p><pre class="programlisting"><em><span class="remark">lang-dart |   439         <code class="literal">Uri()</code> <span class="keep-together">construct
      or</span>:</p><pre class="programlisting"><em><span class="remark">lang-dart | 
|   440 code/ch03/uriFromComponents.dart |   440 code/ch03/uriFromComponents.dart | 
|   441 </span></em>main() { |   441 </span></em>main() { | 
|   442   var uri = new Uri(scheme: 'http', host: 'example.org', |   442   var uri = new Uri(scheme: 'http', host: 'example.org',  | 
|   443                     path: '/foo/bar', fragment: 'frag'); |   443                     path: '/foo/bar', fragment: 'frag'); | 
|   444   assert(uri.toString() == 'http://example.org/foo/bar#frag'); |   444   assert(uri.toString() == 'http://example.org/foo/bar#frag'); | 
|   445 }</pre></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class
      ="title"><a name="ch03-dates-and-times"></a>Dates and Times</h3></div></div></di
      v><p>A DateTime object is a point in time. The time zone is either UTC |   445 }</pre></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class
      ="title"><a name="ch03-dates-and-times"></a>Dates and Times</h3></div></div></di
      v><p>A DateTime object is a point in time. The time zone is either UTC | 
|   446       or the local time zone.</p><p>You can create DateTime objects using severa
      l constructors:</p><pre class="screen"><em><span class="remark">lang-dart |   446       or the local time zone.</p><p>You can create DateTime objects using severa
      l constructors:</p><pre class="screen"><em><span class="remark">lang-dart | 
|   447 </span></em><em><span class="remark">ch03/date.dart |   447 </span></em><em><span class="remark">ch03/date.dart | 
|   448 </span></em>// Get the current date and time. |   448 </span></em>// Get the current date and time. | 
|   449 var now = new DateTime.now(); |   449 var now = new DateTime.now(); | 
|   450  |   450  | 
|   451 // Create a new DateTime with the local time zone. |   451 // Create a new DateTime with the local time zone. | 
|   452 var y2k = new DateTime(2000);   // January 1, 2000 |   452 var y2k = new DateTime(2000);   // January 1, 2000 | 
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   702 </span></em>import 'dart:math' as math;</pre><div class="sect2"><div class="titl
      epage"><div><div><h3 class="title"><a name="ch03-trigonometry"></a>Trigonometry<
      /h3></div></div></div><p>The Math library provides basic trigonometric functions
      :</p><pre class="screen"><em><span class="remark">lang-dart |   702 </span></em>import 'dart:math' as math;</pre><div class="sect2"><div class="titl
      epage"><div><div><h3 class="title"><a name="ch03-trigonometry"></a>Trigonometry<
      /h3></div></div></div><p>The Math library provides basic trigonometric functions
      :</p><pre class="screen"><em><span class="remark">lang-dart | 
|   703 </span></em><em><span class="remark">ch03/math-tests.dart |   703 </span></em><em><span class="remark">ch03/math-tests.dart | 
|   704 </span></em>// Cosine |   704 </span></em>// Cosine | 
|   705 assert(math.cos(math.PI) == -1.0); |   705 assert(math.cos(math.PI) == -1.0); | 
|   706  |   706  | 
|   707 // Sine |   707 // Sine | 
|   708 var degrees = 30; |   708 var degrees = 30; | 
|   709 var radians = degrees * (math.PI / 180); |   709 var radians = degrees * (math.PI / 180); | 
|   710 // radians is now 0.52359. |   710 // radians is now 0.52359. | 
|   711 var sinOf30degrees = math.sin(radians); |   711 var sinOf30degrees = math.sin(radians); | 
|   712  |   712 assert((sinOf30degrees - 0.5).abs() < 0.01); // sin 30° = 0.5</pre><div class
      ="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note<
      /h3><p>These functions use radians, not degrees!</p></div></div><div class="sect
      2"><div class="titlepage"><div><div><h3 class="title"><a name="ch03-maximum-and-
      minimum"></a>Maximum and Minimum</h3></div></div></div><p>The Math library provi
      des <code class="literal">max()</code> and | 
|   713 // Truncate the decimal places to 2. |  | 
|   714 assert(double.parse(sinOf30degrees.toStringAsPrecision(2)) == 0.5);</pre><div cl
      ass="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">No
      te</h3><p>These functions use radians, not degrees!</p></div></div><div class="s
      ect2"><div class="titlepage"><div><div><h3 class="title"><a name="ch03-maximum-a
      nd-minimum"></a>Maximum and Minimum</h3></div></div></div><p>The Math library pr
      ovides <code class="literal">max()</code> and |  | 
|   715       <code class="literal">min()</code> methods:</p><pre class="screen"><em><sp
      an class="remark">lang-dart |   713       <code class="literal">min()</code> methods:</p><pre class="screen"><em><sp
      an class="remark">lang-dart | 
|   716 </span></em><em><span class="remark">ch03/math-tests.dart |   714 </span></em><em><span class="remark">ch03/math-tests.dart | 
|   717 </span></em>assert(math.max(1, 1000) == 1000); |   715 </span></em>assert(math.max(1, 1000) == 1000); | 
|   718 assert(math.min(1, -1000) == -1000);</pre></div><div class="sect2"><div class="t
      itlepage"><div><div><h3 class="title"><a name="ch03-math-constants"></a>Math Con
      stants</h3></div></div></div><p>Find your favorite constants—<span class="emphas
      is"><em>pi</em></span>, |   716 assert(math.min(1, -1000) == -1000);</pre></div><div class="sect2"><div class="t
      itlepage"><div><div><h3 class="title"><a name="ch03-math-constants"></a>Math Con
      stants</h3></div></div></div><p>Find your favorite constants—<span class="emphas
      is"><em>pi</em></span>, | 
|   719       <span class="emphasis"><em>e</em></span>, and more—in the Math library:</p
      ><pre class="screen"><em><span class="remark">lang-dart |   717       <span class="emphasis"><em>e</em></span>, and more—in the Math library:</p
      ><pre class="screen"><em><span class="remark">lang-dart | 
|   720 </span></em><em><span class="remark">ch03/math-tests.dart |   718 </span></em><em><span class="remark">ch03/math-tests.dart | 
|   721 </span></em>// See the Math library for additional constants. |   719 </span></em>// See the Math library for additional constants. | 
|   722 print(math.E);     // 2.718281828459045 |   720 print(math.E);     // 2.718281828459045 | 
|   723 print(math.PI);    // 3.141592653589793 |   721 print(math.PI);    // 3.141592653589793 | 
|   724 print(math.SQRT2); // 1.4142135623730951</pre></div><div class="sect2"><div clas
      s="titlepage"><div><div><h3 class="title"><a name="ch03-random-numbers"></a>Rand
      om Numbers</h3></div></div></div><p>Generate random numbers with the <a class="u
      link" href="http://api.dartlang.org/dart_math/Random.html" target="_top">Random<
      /a> |   722 print(math.SQRT2); // 1.4142135623730951</pre></div><div class="sect2"><div clas
      s="titlepage"><div><div><h3 class="title"><a name="ch03-random-numbers"></a>Rand
      om Numbers</h3></div></div></div><p>Generate random numbers with the <a class="u
      link" href="http://api.dartlang.org/dart_math/Random.html" target="_top">Random<
      /a> | 
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   789         can use AnchorElement’s <code class="literal">href</code> |   787         can use AnchorElement’s <code class="literal">href</code> | 
|   790         property:</p><pre class="screen"><em><span class="remark">lang-dart |   788         property:</p><pre class="screen"><em><span class="remark">lang-dart | 
|   791 </span></em><em><span class="remark">ch03_html/ch03_html.dart |   789 </span></em><em><span class="remark">ch03_html/ch03_html.dart | 
|   792 </span></em>querySelector('#example').href = 'http://dartlang.org';</pre><p>Ofte
      n you need to set properties on multiple elements. For |   790 </span></em>querySelector('#example').href = 'http://dartlang.org';</pre><p>Ofte
      n you need to set properties on multiple elements. For | 
|   793         example, the following code sets the <code class="literal">hidden</code>
       property of all elements that have a |   791         example, the following code sets the <code class="literal">hidden</code>
       property of all elements that have a | 
|   794         class of “mac”, “win”, or “linux”. Setting the <code class="literal">hid
      den</code> property to true has the same effect |   792         class of “mac”, “win”, or “linux”. Setting the <code class="literal">hid
      den</code> property to true has the same effect | 
|   795         as adding <code class="literal">display:none</code> to <span class="keep
      -together">the CSS</span>.</p><pre class="screen"><em><span class="remark">lang-
      dart |   793         as adding <code class="literal">display:none</code> to <span class="keep
      -together">the CSS</span>.</p><pre class="screen"><em><span class="remark">lang-
      dart | 
|   796 </span></em><em><span class="remark">ch03_html/ch03_html.dart |   794 </span></em><em><span class="remark">ch03_html/ch03_html.dart | 
|   797 </span></em><!-- In HTML: --> |   795 </span></em><!-- In HTML: --> | 
|   798 <p> |   796 <p> | 
|   799   <span class="os linux">Words for Linux</span> |   797   <span class="linux">Words for Linux</span> | 
|   800   <span class="os mac">Words for Mac</span> |   798   <span class="macos">Words for Mac</span> | 
|   801   <span class="os win">Words for Windows</span> |   799   <span class="windows">Words for Windows</span> | 
|   802 </p> |   800 </p> | 
|   803 <em><span class="remark"> |   801 <em><span class="remark"> | 
|   804 </span></em>// In Dart: |   802 </span></em>// In Dart: | 
|   805 final osList = ['mac', 'win', 'linux']; |   803 final osList = ['macos', 'windows', 'linux']; | 
|   806  |   804  | 
|   807 var userOs = 'linux'; // In real code you'd programmatically determine this. |   805 var userOs = 'linux'; // In real code you'd programmatically determine this. | 
|   808  |   806  | 
|   809 for (var os in osList) {            // For each possible OS... |   807 for (var os in osList) {            // For each possible OS... | 
|   810   bool shouldShow = (os == userOs); // Does this OS match the user's OS? |   808   bool shouldShow = (os == userOs); // Does this OS match the user's OS? | 
|   811   for (var elem in querySelectorAll('.$os')) { // Find all elements for this OS. |   809  | 
 |   810   // Find all elements with class=os. For example, if os == 'windows', call | 
 |   811   // querySelectorAll('.windows') to find all elements with the class "windows". | 
 |   812   for (var elem in querySelectorAll('.$os')) { // String interpolation ($os) | 
|   812     elem.hidden = !shouldShow;      // Show or hide each element. |   813     elem.hidden = !shouldShow;      // Show or hide each element. | 
|   813   } |   814   } | 
|   814 }</pre><p>When the right property isn’t available or convenient, you can |   815 }</pre><p>When the right property isn’t available or convenient, you can | 
|   815         use Element’s <code class="literal">attributes</code> property. |   816         use Element’s <code class="literal">attributes</code> property. | 
|   816         This property is a <code class="literal">Map<String, String></code
      >, where |   817         This property is a <code class="literal">Map<String, String></code
      >, where | 
|   817         the keys are attribute names. For a list of attribute names and their |   818         the keys are attribute names. For a list of attribute names and their | 
|   818         meanings, see the <a class="ulink" href="https://developer.mozilla.org/e
      n/HTML/Attributes" target="_top">MDN Attributes |   819         meanings, see the <a class="ulink" href="https://developer.mozilla.org/e
      n/HTML/Attributes" target="_top">MDN Attributes | 
|   819         page.</a> Here’s an example of setting an attribute’s |   820         page.</a> Here’s an example of setting an attribute’s | 
|   820         value:</p><pre class="screen"><em><span class="remark">lang-dart |   821         value:</p><pre class="screen"><em><span class="remark">lang-dart | 
|   821 </span></em><em><span class="remark">ch03_html/ch03_html.dart |   822 </span></em><em><span class="remark">ch03_html/ch03_html.dart | 
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1003   } |  1004   } | 
|  1004 } |  1005 } | 
|  1005  |  1006  | 
|  1006 main() { |  1007 main() { | 
|  1007   var dataUrl = '/registrations/create'; |  1008   var dataUrl = '/registrations/create'; | 
|  1008   var data = {'dart': 'fun', 'editor': 'productive'}; |  1009   var data = {'dart': 'fun', 'editor': 'productive'}; | 
|  1009   var encodedData = encodeMap(data); |  1010   var encodedData = encodeMap(data); | 
|  1010  |  1011  | 
|  1011   var httpRequest = new HttpRequest(); |  1012   var httpRequest = new HttpRequest(); | 
|  1012   httpRequest.open('POST', dataUrl); |  1013   httpRequest.open('POST', dataUrl); | 
|  1013   httpRequest.setRequestHeader('Content-type', |  1014   httpRequest.setRequestHeader('Content-type',  | 
|  1014                                'application/x-www-form-urlencoded'); |  1015                                'application/x-www-form-urlencoded'); | 
|  1015   httpRequest.onLoadEnd.listen((e) => loadEnd(httpRequest)); |  1016   httpRequest.onLoadEnd.listen((e) => loadEnd(httpRequest)); | 
|  1016   httpRequest.send(encodedData); |  1017   httpRequest.send(encodedData); | 
|  1017 }</pre></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class
      ="title"><a name="ch03-websockets"></a>Sending and Receiving Real-Time Data with
       WebSockets</h3></div></div></div><p>A WebSocket allows your web app to exchange
       data with a server |  1018 }</pre></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class
      ="title"><a name="ch03-websockets"></a>Sending and Receiving Real-Time Data with
       WebSockets</h3></div></div></div><p>A WebSocket allows your web app to exchange
       data with a server | 
|  1018       interactively—no polling necessary. A server creates the WebSocket and |  1019       interactively—no polling necessary. A server creates the WebSocket and | 
|  1019       listens for requests on a URL that starts with <span class="strong"><stron
      g>ws://</strong></span>—for example, ws://127.0.0.1:1337/ws. The |  1020       listens for requests on a URL that starts with <span class="strong"><stron
      g>ws://</strong></span>—for example, ws://127.0.0.1:1337/ws. The | 
|  1020       data transmitted over a WebSocket can be a string, a blob, or an <a class=
      "ulink" href="http://api.dartlang.org/html/ArrayBuffer.html" target="_top">Array
      Buffer.</a> |  1021       data transmitted over a WebSocket can be a string, a blob, or an <a class=
      "ulink" href="http://api.dartlang.org/html/ArrayBuffer.html" target="_top">Array
      Buffer.</a> | 
|  1021       Often, the data is a JSON-formatted string.</p><p>To use a WebSocket in yo
      ur web app, first create a <a class="ulink" href="http://api.dartlang.org/html/W
      ebSocket.html" target="_top">WebSocket</a> |  1022       Often, the data is a JSON-formatted string.</p><p>To use a WebSocket in yo
      ur web app, first create a <a class="ulink" href="http://api.dartlang.org/html/W
      ebSocket.html" target="_top">WebSocket</a> | 
|  1022       object, passing the WebSocket URL as an argument:</p><pre class="screen"><
      em><span class="remark">lang-dart |  1023       object, passing the WebSocket URL as an argument:</p><pre class="screen"><
      em><span class="remark">lang-dart | 
|  1023 github.com/dart-lang/dart-samples/.../html5/web/websockets/basics/websocket_samp
      le.dart |  1024 github.com/dart-lang/dart-samples/.../web/html5/websockets/basics/websocket_samp
      le.dart | 
|  1024 </span></em>var ws = new WebSocket('ws://echo.websocket.org');</pre><div class="
      sect3"><div class="titlepage"><div><div><h4 class="title"><a name="ch03-sending-
      data"></a>Sending data</h4></div></div></div><p>To send string data on the WebSo
      cket, use the |  1025 </span></em>var ws = new WebSocket('ws://echo.websocket.org');</pre><div class="
      sect3"><div class="titlepage"><div><div><h4 class="title"><a name="ch03-sending-
      data"></a>Sending data</h4></div></div></div><p>To send string data on the WebSo
      cket, use the | 
|  1025         <code class="literal">send()</code> method:</p><pre class="screen"><em><
      span class="remark">lang-dart |  1026         <code class="literal">send()</code> method:</p><pre class="screen"><em><
      span class="remark">lang-dart | 
|  1026 </span></em><em><span class="remark">github.com/dart-lang/dart-samples/.../html5
      /web/websockets/basics/websocket_sample.dart |  1027 </span></em><em><span class="remark">github.com/dart-lang/dart-samples/.../web/h
      tml5/websockets/basics/websocket_sample.dart | 
|  1027 </span></em>ws.send('Hello from Dart!');</pre></div><div class="sect3"><div clas
      s="titlepage"><div><div><h4 class="title"><a name="ch03-receiving-data"></a>Rece
      iving data</h4></div></div></div><p>To receive data on the WebSocket, register a
       listener for |  1028 </span></em>ws.send('Hello from Dart!');</pre></div><div class="sect3"><div clas
      s="titlepage"><div><div><h4 class="title"><a name="ch03-receiving-data"></a>Rece
      iving data</h4></div></div></div><p>To receive data on the WebSocket, register a
       listener for | 
|  1028         message events:</p><pre class="screen"><em><span class="remark">lang-dar
      t |  1029         message events:</p><pre class="screen"><em><span class="remark">lang-dar
      t | 
|  1029 <em><span class="remark">github.com/dart-lang/dart-samples/.../html5/web/websock
      ets/basics/websocket_sample.dart |  1030 <em><span class="remark">github.com/dart-lang/dart-samples/.../web/html5/websock
      ets/basics/websocket_sample.dart | 
|  1030 </span></em></span></em>ws.onMessage.listen((MessageEvent e) { |  1031 </span></em></span></em>ws.onMessage.listen((MessageEvent e) { | 
|  1031   print('Received message: ${e.data}'); |  1032   print('Received message: ${e.data}'); | 
|  1032 });</pre><p>The message event handler receives a <a class="ulink" href="http://a
      pi.dartlang.org/html/MessageEvent.html" target="_top">MessageEvent</a> |  1033 });</pre><p>The message event handler receives a <a class="ulink" href="http://a
      pi.dartlang.org/html/MessageEvent.html" target="_top">MessageEvent</a> | 
|  1033         object. This object’s <code class="literal">data</code> field |  1034         object. This object’s <code class="literal">data</code> field | 
|  1034         has the data from the server.</p></div><div class="sect3"><div class="ti
      tlepage"><div><div><h4 class="title"><a name="ch03-handling-websocket-events"></
      a>Handling WebSocket events</h4></div></div></div><p>Your app can handle the fol
      lowing WebSocket events: open, close, |  1035         has the data from the server.</p></div><div class="sect3"><div class="ti
      tlepage"><div><div><h4 class="title"><a name="ch03-handling-websocket-events"></
      a>Handling WebSocket events</h4></div></div></div><p>Your app can handle the fol
      lowing WebSocket events: open, close, | 
|  1035         error, and (as shown earlier) message. Here’s an example of a method |  1036         error, and (as shown earlier) message. Here’s an example of a method | 
|  1036         that creates a WebSocket object and registers handlers for open, |  1037         that creates a WebSocket object and registers handlers for open, | 
|  1037         close, error, and message events:</p><pre class="screen"><em><span class
      ="remark">lang-dart |  1038         close, error, and message events:</p><pre class="screen"><em><span class
      ="remark">lang-dart | 
|  1038 <em><span class="remark">github.com/dart-lang/dart-samples/.../html5/web/websock
      ets/basics/websocket_sample.dart |  1039 <em><span class="remark">github.com/dart-lang/dart-samples/.../web/html5/websock
      ets/basics/websocket_sample.dart | 
|  1039 </span></em></span></em>void initWebSocket([int retrySeconds = 2]) { |  1040 </span></em></span></em>void initWebSocket([int retrySeconds = 2]) { | 
|  1040   var reconnectScheduled = false; |  1041   var reconnectScheduled = false; | 
|  1041  |  1042  | 
|  1042   print("Connecting to websocket"); |  1043   print("Connecting to websocket"); | 
|  1043   ws = new WebSocket('ws://echo.websocket.org'); |  1044   ws = new WebSocket('ws://echo.websocket.org'); | 
|  1044  |  1045  | 
|  1045   void scheduleReconnect() { |  1046   void scheduleReconnect() { | 
|  1046     if (!reconnectScheduled) { |  1047     if (!reconnectScheduled) { | 
|  1047       new Timer(new Duration(milliseconds: 1000 * retrySeconds), |  1048       new Timer(new Duration(milliseconds: 1000 * retrySeconds),  | 
|  1048                 () => initWebSocket(retrySeconds * 2)); |  1049                 () => initWebSocket(retrySeconds * 2)); | 
|  1049     } |  1050     } | 
|  1050     reconnectScheduled = true; |  1051     reconnectScheduled = true; | 
|  1051   } |  1052   } | 
|  1052  |  1053  | 
|  1053   ws.onOpen.listen((e) { |  1054   ws.onOpen.listen((e) { | 
|  1054     print('Connected'); |  1055     print('Connected'); | 
|  1055     ws.send('Hello from Dart!'); |  1056     ws.send('Hello from Dart!'); | 
|  1056   }); |  1057   }); | 
|  1057  |  1058  | 
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1145 import 'dart:async'; |  1146 import 'dart:async'; | 
|  1146  |  1147  | 
|  1147 main() { |  1148 main() { | 
|  1148   var config = new File('config.txt'); |  1149   var config = new File('config.txt'); | 
|  1149   Stream<List<int>> inputStream = config.openRead(); |  1150   Stream<List<int>> inputStream = config.openRead(); | 
|  1150  |  1151  | 
|  1151   inputStream |  1152   inputStream | 
|  1152     .transform(UTF8.decoder) |  1153     .transform(UTF8.decoder) | 
|  1153     .transform(new LineSplitter()) |  1154     .transform(new LineSplitter()) | 
|  1154     .listen( |  1155     .listen( | 
|  1155       (String line) { |  1156       (String line) {  | 
|  1156         print('Got ${line.length} characters from stream'); |  1157         print('Got ${line.length} characters from stream'); | 
|  1157       }, |  1158       }, | 
|  1158       onDone: () { print('file is now closed'); }, |  1159       onDone: () { print('file is now closed'); }, | 
|  1159       onError: (e) { print(e.toString()); }); |  1160       onError: (e) { print(e.toString()); }); | 
|  1160 }</pre></div><div class="sect3"><div class="titlepage"><div><div><h4 class="titl
      e"><a name="ch03-writing-file-contents"></a>Writing file contents</h4></div></di
      v></div><p>You can use an <a class="ulink" href="http://api.dartlang.org/dart_io
      /IOSink.html" target="_top">IOSink</a> to |  1161 }</pre></div><div class="sect3"><div class="titlepage"><div><div><h4 class="titl
      e"><a name="ch03-writing-file-contents"></a>Writing file contents</h4></div></di
      v></div><p>You can use an <a class="ulink" href="http://api.dartlang.org/dart_io
      /IOSink.html" target="_top">IOSink</a> to | 
|  1161         write data to a file. Use the File <code class="literal">openWrite()</co
      de> |  1162         write data to a file. Use the File <code class="literal">openWrite()</co
      de> | 
|  1162         method to get an IOSink that you can write to. The default mode, |  1163         method to get an IOSink that you can write to. The default mode, | 
|  1163         <code class="literal">FileMode.WRITE</code>, completely |  1164         <code class="literal">FileMode.WRITE</code>, completely | 
|  1164         overwrites existing data in the file.</p><pre class="programlisting"><em
      ><span class="remark">lang-dart |  1165         overwrites existing data in the file.</p><pre class="programlisting"><em
      ><span class="remark">lang-dart | 
|  1165 </span></em><em><span class="remark">ch03/writeFile.dart |  1166 </span></em><em><span class="remark">ch03/writeFile.dart | 
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1212 </span></em>import 'dart:io'; |  1213 </span></em>import 'dart:io'; | 
|  1213  |  1214  | 
|  1214 main() { |  1215 main() { | 
|  1215   dartHandler(HttpRequest request) { |  1216   dartHandler(HttpRequest request) { | 
|  1216     request.response.headers.contentType = new ContentType('text', 'plain'); |  1217     request.response.headers.contentType = new ContentType('text', 'plain'); | 
|  1217     request.response.write('Dart is optionally typed'); |  1218     request.response.write('Dart is optionally typed'); | 
|  1218     request.response.close(); |  1219     request.response.close(); | 
|  1219   }; |  1220   }; | 
|  1220  |  1221  | 
|  1221   HttpServer.bind('127.0.0.1', 8888).then((HttpServer server) { |  1222   HttpServer.bind('127.0.0.1', 8888).then((HttpServer server) { | 
|  1222     server.listen((request) { |  1223     server.listen((request) {  | 
|  1223       print('Got request for ${request.uri.path}'); |  1224       print('Got request for ${request.uri.path}'); | 
|  1224       if (request.uri.path == '/languages/dart') { |  1225       if (request.uri.path == '/languages/dart') { | 
|  1225         dartHandler(request); |  1226         dartHandler(request); | 
|  1226       } else { |  1227       } else { | 
|  1227         request.response.write('Not found'); |  1228         request.response.write('Not found'); | 
|  1228         request.response.close(); |  1229         request.response.close(); | 
|  1229       } |  1230       } | 
|  1230     }); |  1231     }); | 
|  1231   }); |  1232   }); | 
|  1232 }</pre><p>For a more comprehensive HTTP server, see <a class="xref" href="ch05.h
      tml" title="Chapter 5. Walkthrough: Dartiverse Search">Chapter 5, <i>Walkthrough
      : Dartiverse Search</i></a>. It features the Dartiverse Search sample, which |  1233 }</pre><p>For a more comprehensive HTTP server, see <a class="xref" href="ch05.h
      tml" title="Chapter 5. Walkthrough: Dartiverse Search">Chapter 5, <i>Walkthrough
      : Dartiverse Search</i></a>. It features the Dartiverse Search sample, which | 
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1323   print(string); // 'Îñţérñåţîöñåļîžåţîờñ' |  1324   print(string); // 'Îñţérñåţîöñåļîžåţîờñ' | 
|  1324 } |  1325 } | 
|  1325 </pre><p>To convert a stream of UTF-8 characters into a Dart string, |  1326 </pre><p>To convert a stream of UTF-8 characters into a Dart string, | 
|  1326       specify <code class="literal">UTF8.decoder</code> to the Stream |  1327       specify <code class="literal">UTF8.decoder</code> to the Stream | 
|  1327       <code class="literal">transform()</code> method:</p><pre class="screen"><e
      m><span class="remark">lang-dart |  1328       <code class="literal">transform()</code> method:</p><pre class="screen"><e
      m><span class="remark">lang-dart | 
|  1328 ch03/readFile.dart |  1329 ch03/readFile.dart | 
|  1329 </span></em>inputStream |  1330 </span></em>inputStream | 
|  1330   .transform(UTF8.decoder) |  1331   .transform(UTF8.decoder) | 
|  1331   .transform(new LineSplitter()) |  1332   .transform(new LineSplitter()) | 
|  1332   .listen( |  1333   .listen( | 
|  1333     (String line) { |  1334     (String line) {  | 
|  1334       print('Read ${line.length} bytes from stream'); |  1335       print('Read ${line.length} bytes from stream'); | 
|  1335     });</pre><p>Use <code class="literal">UTF8.encode()</code> to encode a Dart 
      string as a |  1336     });</pre><p>Use <code class="literal">UTF8.encode()</code> to encode a Dart 
      string as a | 
|  1336       list of UTF8-encoded bytes:</p><pre class="screen"><em><span class="remark
      ">lang-dart |  1337       list of UTF8-encoded bytes:</p><pre class="screen"><em><span class="remark
      ">lang-dart | 
|  1337 ch03/encodeUtf8.dart |  1338 ch03/encodeUtf8.dart | 
|  1338 </span></em>import 'dart:convert' show UTF8; |  1339 </span></em>import 'dart:convert' show UTF8; | 
|  1339  |  1340  | 
|  1340 main() { |  1341 main() { | 
|  1341   List<int> expected = [0xc3, 0x8e, 0xc3, 0xb1, 0xc5, 0xa3, 0xc3, 0xa9, 0x
      72, |  1342   List<int> expected = [0xc3, 0x8e, 0xc3, 0xb1, 0xc5, 0xa3, 0xc3, 0xa9, 0x
      72, | 
|  1342                         0xc3, 0xb1, 0xc3, 0xa5, 0xc5, 0xa3, 0xc3, 0xae, 0xc3, |  1343                         0xc3, 0xb1, 0xc3, 0xa5, 0xc5, 0xa3, 0xc3, 0xae, 0xc3, | 
|  1343                         0xb6, 0xc3, 0xb1, 0xc3, 0xa5, 0xc4, 0xbc, 0xc3, 0xae, |  1344                         0xb6, 0xc3, 0xb1, 0xc3, 0xa5, 0xc4, 0xbc, 0xc3, 0xae, | 
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1388 const className = #MyClass; |  1389 const className = #MyClass; | 
|  1389 assert('MyClass' == MirrorSystem.getName(className)); |  1390 assert('MyClass' == MirrorSystem.getName(className)); | 
|  1390 </pre></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title
      "><a name="ch03-mirrors-introspection"></a>Introspection</h3></div></div></div><
      p>Use mirrors to introspect the running program's structure. You can |  1391 </pre></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title
      "><a name="ch03-mirrors-introspection"></a>Introspection</h3></div></div></div><
      p>Use mirrors to introspect the running program's structure. You can | 
|  1391       inspect classes, libraries, instances, and more.</p><p>The examples in thi
      s section use the following Person |  1392       inspect classes, libraries, instances, and more.</p><p>The examples in thi
      s section use the following Person | 
|  1392       class:</p><pre class="screen"><em><span class="remark">lang-dart |  1393       class:</p><pre class="screen"><em><span class="remark">lang-dart | 
|  1393 ch03/mirrors.dart |  1394 ch03/mirrors.dart | 
|  1394 </span></em>class Person { |  1395 </span></em>class Person { | 
|  1395   String firstName; |  1396   String firstName; | 
|  1396   String lastName; |  1397   String lastName; | 
|  1397   int age; |  1398   int age; | 
|  1398  |  1399    | 
|  1399   Person(this.firstName, this.lastName, this.age); |  1400   Person(this.firstName, this.lastName, this.age); | 
|  1400  |  1401  | 
|  1401   String get fullName => '$firstName $lastName'; |  1402   String get fullName => '$firstName $lastName'; | 
|  1402  |  1403  | 
|  1403   void greet(String other) { |  1404   void greet(String other) { | 
|  1404     print('Hello there, $other!'); |  1405     print('Hello there, $other!'); | 
|  1405   } |  1406   } | 
|  1406 }</pre><p>To begin, you need to <span class="emphasis"><em>reflect</em></span> o
      n a class or |  1407 }</pre><p>To begin, you need to <span class="emphasis"><em>reflect</em></span> o
      n a class or | 
|  1407       object to get its <span class="emphasis"><em>mirror</em></span>.</p><div c
      lass="sect3"><div class="titlepage"><div><div><h4 class="title"><a name="ch03-mi
      rrors-class-inspect"></a>Class Mirrors</h4></div></div></div><p>Reflect on a Typ
      e to get its ClassMirror.</p><pre class="screen"><em><span class="remark">lang-d
      art |  1408       object to get its <span class="emphasis"><em>mirror</em></span>.</p><div c
      lass="sect3"><div class="titlepage"><div><div><h4 class="title"><a name="ch03-mi
      rrors-class-inspect"></a>Class Mirrors</h4></div></div></div><p>Reflect on a Typ
      e to get its ClassMirror.</p><pre class="screen"><em><span class="remark">lang-d
      art | 
|  1408 ch03/mirrors.dart |  1409 ch03/mirrors.dart | 
|  1409 </span></em>ClassMirror mirror = reflectClass(Person); |  1410 </span></em>ClassMirror mirror = reflectClass(Person); | 
|  1410  |  1411  | 
|  1411 assert('Person' == MirrorSystem.getName(mirror.simpleName));</pre><p>You can als
      o call <code class="literal">runtimeType</code> to get a Type |  1412 assert('Person' == MirrorSystem.getName(mirror.simpleName));</pre><p>You can als
      o call <code class="literal">runtimeType</code> to get a Type | 
|  1412         from an instance.</p><pre class="screen"><em><span class="remark">lang-d
      art |  1413         from an instance.</p><pre class="screen"><em><span class="remark">lang-d
      art | 
|  1413 ch03/mirrors.dart |  1414 ch03/mirrors.dart | 
|  1414 </span></em>var person = new Person('Bob', 'Smith', 33); |  1415 </span></em>var person = new Person('Bob', 'Smith', 33); | 
|  1415 ClassMirror mirror = reflectClass(person.runtimeType); |  1416 ClassMirror mirror = reflectClass(person.runtimeType); | 
|  1416  |  1417  | 
|  1417 assert('Person' == MirrorSystem.getName(mirror.simpleName));</pre><p>Once you ha
      ve a ClassMirror, you can get a class's constructors, |  1418 assert('Person' == MirrorSystem.getName(mirror.simpleName));</pre><p>Once you ha
      ve a ClassMirror, you can get a class's constructors, | 
|  1418         fields, and more. Here is an example of listing the constructors of a |  1419         fields, and more. Here is an example of listing the constructors of a | 
|  1419         class.</p><pre class="screen"><em><span class="remark">lang-dart |  1420         class.</p><pre class="screen"><em><span class="remark">lang-dart | 
|  1420 ch03/mirrors.dart |  1421 ch03/mirrors.dart | 
|  1421 </span></em>showConstructors(ClassMirror mirror) { |  1422 </span></em>showConstructors(ClassMirror mirror) { | 
|  1422   var constructors = mirror.declarations.values |  1423   var constructors = mirror.declarations.values | 
|  1423       .where((m) => m is MethodMirror && m.isConstructor); |  1424       .where((m) => m is MethodMirror && m.isConstructor); | 
|  1424  |  1425    | 
|  1425   constructors.forEach((m) { |  1426   constructors.forEach((m) { | 
|  1426     print('The constructor ${m.simpleName} has ' |  1427     print('The constructor ${m.simpleName} has ' | 
|  1427           '${m.parameters.length} parameters.'); |  1428           '${m.parameters.length} parameters.'); | 
|  1428   }); |  1429   }); | 
|  1429 }</pre><p>Here is an example of listing all of the fields declared by a |  1430 }</pre><p>Here is an example of listing all of the fields declared by a | 
|  1430         class.</p><pre class="screen"><em><span class="remark">lang-dart |  1431         class.</p><pre class="screen"><em><span class="remark">lang-dart | 
|  1431 ch03/mirrors.dart |  1432 ch03/mirrors.dart | 
|  1432 </span></em>showFields(ClassMirror mirror) { |  1433 </span></em>showFields(ClassMirror mirror) { | 
|  1433   var fields = mirror.declarations.values.where((m) => m is VariableMirror); |  1434   var fields = mirror.declarations.values.where((m) => m is VariableMirror); | 
|  1434  |  1435  | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
|  1462 mirror.invoke(#greet, ['Shailen']);</pre></div><div class="sect3"><div class="ti
      tlepage"><div><div><h4 class="title"><a name="ch03-mirrors-getset-fields"></a>In
      voke Getters and Setters</h4></div></div></div><p>Use InstanceMirror's <code cla
      ss="literal">getField()</code> and |  1463 mirror.invoke(#greet, ['Shailen']);</pre></div><div class="sect3"><div class="ti
      tlepage"><div><div><h4 class="title"><a name="ch03-mirrors-getset-fields"></a>In
      voke Getters and Setters</h4></div></div></div><p>Use InstanceMirror's <code cla
      ss="literal">getField()</code> and | 
|  1463         <code class="literal">setField()</code> methods to get and set propertie
      s of an |  1464         <code class="literal">setField()</code> methods to get and set propertie
      s of an | 
|  1464         object.</p><pre class="screen"><em><span class="remark">lang-dart |  1465         object.</p><pre class="screen"><em><span class="remark">lang-dart | 
|  1465 ch03/mirrors.dart</span></em> |  1466 ch03/mirrors.dart</span></em> | 
|  1466 var p = new Person('Bob', 'Smith', 42); |  1467 var p = new Person('Bob', 'Smith', 42); | 
|  1467 InstanceMirror mirror = reflect(p); |  1468 InstanceMirror mirror = reflect(p); | 
|  1468  |  1469  | 
|  1469 // Get the value of a property. |  1470 // Get the value of a property. | 
|  1470 var fullName = mirror.getField(#fullName).reflectee; |  1471 var fullName = mirror.getField(#fullName).reflectee; | 
|  1471 assert(fullName == 'Bob Smith'); |  1472 assert(fullName == 'Bob Smith'); | 
|  1472  |  1473    | 
|  1473 // Set the value of a property. |  1474 // Set the value of a property. | 
|  1474 mirror.setField(#firstName, 'Mary'); |  1475 mirror.setField(#firstName, 'Mary'); | 
|  1475 assert(p.firstName == 'Mary');</pre></div></div><div class="sect2"><div class="t
      itlepage"><div><div><h3 class="title"><a name="ch03-more-information-mirrors"></
      a>More Information</h3></div></div></div><p>The article <a class="ulink" href="h
      ttps://www.dartlang.org/articles/reflection-with-mirrors/" target="_top">Reflect
      ion |  1476 assert(p.firstName == 'Mary');</pre></div></div><div class="sect2"><div class="t
      itlepage"><div><div><h3 class="title"><a name="ch03-more-information-mirrors"></
      a>More Information</h3></div></div></div><p>The article <a class="ulink" href="h
      ttps://www.dartlang.org/articles/reflection-with-mirrors/" target="_top">Reflect
      ion | 
|  1476       in Dart with Mirrors</a> has more information and examples. Also see |  1477       in Dart with Mirrors</a> has more information and examples. Also see | 
|  1477       the API docs for <a class="ulink" href="http://api.dartlang.org/dart_mirro
      rs.html" target="_top">dart:mirror,</a> |  1478       the API docs for <a class="ulink" href="http://api.dartlang.org/dart_mirro
      rs.html" target="_top">dart:mirror,</a> | 
|  1478       especially <a class="ulink" href="http://api.dartlang.org/dart_mirrors/Mir
      rorsUsed.html" target="_top">MirrorsUsed</a>, |  1479       especially <a class="ulink" href="http://api.dartlang.org/dart_mirrors/Mir
      rorsUsed.html" target="_top">MirrorsUsed</a>, | 
|  1479       <a class="ulink" href="http://api.dartlang.org/dart_mirrors/ClassMirror.ht
      ml" target="_top">ClassMirror,</a> |  1480       <a class="ulink" href="http://api.dartlang.org/dart_mirrors/ClassMirror.ht
      ml" target="_top">ClassMirror,</a> | 
|  1480       and <a class="ulink" href="http://api.dartlang.org/dart_mirrors/InstanceMi
      rror.html" target="_top">InstanceMirror.</a></p></div></div><div class="sect1"><
      div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="c
      h03-summary"></a>Summary</h2></div></div></div><p>This chapter introduced you to
       the most commonly used functionality |  1481       and <a class="ulink" href="http://api.dartlang.org/dart_mirrors/InstanceMi
      rror.html" target="_top">InstanceMirror.</a></p></div></div><div class="sect1"><
      div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="c
      h03-summary"></a>Summary</h2></div></div></div><p>This chapter introduced you to
       the most commonly used functionality | 
|  1481     in many of Dart’s built-in libraries. It didn’t cover all the built-in |  1482     in many of Dart’s built-in libraries. It didn’t cover all the built-in | 
|  1482     libraries, however. Others that you might want to look into include <a class
      ="ulink" href="http://api.dartlang.org/dart_collection.html" target="_top">dart:
      collection,</a> |  1483     libraries, however. Others that you might want to look into include <a class
      ="ulink" href="http://api.dartlang.org/dart_collection.html" target="_top">dart:
      collection,</a> | 
|  1483     <a class="ulink" href="http://api.dartlang.org/dart_isolate.html" target="_t
      op">dart:isolate,</a> |  1484     <a class="ulink" href="http://api.dartlang.org/dart_isolate.html" target="_t
      op">dart:isolate,</a> | 
|  1484     <a class="ulink" href="http://api.dartlang.org/dart_js.html" target="_top">d
      art:js,</a> and |  1485     <a class="ulink" href="http://api.dartlang.org/dart_js.html" target="_top">d
      art:js,</a> and | 
|  1485     <a class="ulink" href="http://api.dartlang.org/dart_typed_data.html" target=
      "_top">dart:typed_data.</a> |  1486     <a class="ulink" href="http://api.dartlang.org/dart_typed_data.html" target=
      "_top">dart:typed_data.</a> | 
|  1486     You can get yet more libraries by using the pub tool, discussed in the |  1487     You can get yet more libraries by using the pub tool, discussed in the | 
|  1487     next chapter. The <a class="ulink" href="http://pub.dartlang.org/packages/ar
      gs" target="_top">args,</a> <a class="ulink" href="http://api.dartlang.org/loggi
      ng.html" target="_top">logging,</a> <a class="ulink" href="http://pub.dartlang.o
      rg/packages/polymer" target="_top">polymer,</a> and <a class="ulink" href="http:
      //api.dartlang.org/unittest.html" target="_top"><span class="keep-together">unit
      test</span></a> libraries are just a |  1488     next chapter. The <a class="ulink" href="http://pub.dartlang.org/packages/ar
      gs" target="_top">args,</a> <a class="ulink" href="http://api.dartlang.org/loggi
      ng.html" target="_top">logging,</a> <a class="ulink" href="http://pub.dartlang.o
      rg/packages/polymer" target="_top">polymer,</a> and <a class="ulink" href="http:
      //api.dartlang.org/unittest.html" target="_top"><span class="keep-together">unit
      test</span></a> libraries are just a | 
|  1488     sampling of what you can install using pub.</p></div></div><div class="navfo
      oter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" al
      ign="left"><a accesskey="p" href="ch02.html">Prev</a> </td><td width="20%" align
      ="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch04.html"
      >Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 2. A To
      ur of the Dart Language </td><td width="20%" align="center"><a accesskey="h" hre
      f="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 
      4. Tools</td></tr></table></div> |  1489     sampling of what you can install using pub.</p></div></div><div class="navfo
      oter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" al
      ign="left"><a accesskey="p" href="ch02.html">Prev</a> </td><td width="20%" align
      ="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch04.html"
      >Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 2. A To
      ur of the Dart Language </td><td width="20%" align="center"><a accesskey="h" hre
      f="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 
      4. Tools</td></tr></table></div> | 
| OLD | NEW |