├── code ├── library-tour │ ├── write-file │ │ ├── log.txt │ │ ├── betterLog.txt │ │ ├── copyOfIcon.ico │ │ ├── pubspec.yaml │ │ └── bin │ │ │ ├── icon.ico │ │ │ └── main.dart │ ├── get-data-from-server │ │ ├── web │ │ │ ├── nodata.xml │ │ │ ├── nodata.json │ │ │ ├── baddata.json │ │ │ ├── data.json │ │ │ ├── data.xml │ │ │ ├── styles.css │ │ │ ├── index.html │ │ │ └── main.dart │ │ └── pubspec.yaml │ ├── date │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── map-1 │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── async-await │ │ ├── async-await-web │ │ │ ├── web │ │ │ │ ├── humans.txt │ │ │ │ ├── index.html │ │ │ │ ├── styles.css │ │ │ │ └── main.dart │ │ │ └── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── duration │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── futures │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── iterator │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── map-keys │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── mirrors │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── comparable │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── encode-uri │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── json-parse │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── list-files │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── list-tests │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── math-tests │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── parse-uri │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── read-file │ │ ├── pubspec.yaml │ │ └── bin │ │ │ ├── config.txt │ │ │ ├── binary_read.dart │ │ │ ├── file_errors.dart │ │ │ ├── text_read.dart │ │ │ └── read_file.dart │ ├── set-tests │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── client-server │ │ ├── pubspec.yaml │ │ └── bin │ │ │ ├── main.dart │ │ │ └── http_server.dart │ ├── decode-utf-8 │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── encode-utf-8 │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── number-tests │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── string-tests │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── json-stringify │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── collection-any-every │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── collection-is-empty │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── encode-uri-components │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── uri-from-components │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── collection-apply-function │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── future-then │ │ ├── pubspec.yaml │ │ └── web │ │ │ ├── styles.css │ │ │ ├── index.html │ │ │ └── main.dart │ ├── post-data-to-server │ │ ├── pubspec.yaml │ │ └── web │ │ │ ├── styles.css │ │ │ ├── index.html │ │ │ └── main.dart │ ├── reference │ │ ├── exceptions.dart │ │ ├── spawnIsolate.dart │ │ └── rootIsolate.dart │ └── port-send-receive │ │ ├── port_send.dart │ │ └── port_receive.dart ├── language-tour │ ├── libraries │ │ ├── lib1 │ │ │ ├── pubspec.yaml │ │ │ └── lib │ │ │ │ └── lib1.dart │ │ ├── lib2 │ │ │ ├── pubspec.yaml │ │ │ └── lib │ │ │ │ └── lib2.dart │ │ ├── mylib │ │ │ ├── pubspec.yaml │ │ │ └── lib │ │ │ │ └── mylib.dart │ │ ├── utils │ │ │ ├── pubspec.yaml │ │ │ └── lib │ │ │ │ └── utils.dart │ │ ├── using_libraries.dart │ │ ├── using_schemes.dart │ │ ├── library_partial.dart │ │ ├── pubspec.yaml │ │ └── library_prefix.dart │ ├── args │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── color │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── const │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── op-as │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── point │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── runes │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── generics │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── if-one │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── mixins │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── quoting │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── symbols │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── vector │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── flow-while │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── map-length │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── op-assign │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── op-bitwise │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── op-logical │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── precedence │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── rectangle │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── async-await │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── bit-shifting │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── empty-string │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── enum-switch │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── flow-if-else │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── french-togo │ │ ├── pubspec.yaml │ │ ├── lib │ │ │ ├── togo.dart │ │ │ └── src │ │ │ │ └── french.dart │ │ └── bin │ │ │ └── main.dart │ ├── imposter │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── list-indexing │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── list-literal │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── map-add-item │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── op-equality │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── raw-strings │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── static-types │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── triple-quotes │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── ballgame │ │ ├── ball.dart │ │ ├── util.dart │ │ └── ballgame.dart │ ├── const-vs-const │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── double-literals │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── map-constructor │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── map-missing-key │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── no-such-method │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── number-literals │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── object-classes │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── string-literals │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── basic-dart-program │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── callable-function │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── continue_example │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── flow-switch-case │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── function-closure │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── function-example │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── function-shorthand │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── instance-variables │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── integer-literals │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── map-retrieve-item │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── nested-functions │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── number-conversion │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── sorted-collection │ │ ├── pubspec.yaml │ │ └── bin │ │ │ ├── main.dart │ │ │ ├── sorted_collection_broken_2.dart │ │ │ └── sorted_collection_broken.dart │ ├── while-collection │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── arithmetic-operators │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── creating-a-variable │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── factory-constructor │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── final-initialization │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── function-as-variable │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── function-equality-2 │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── generics-base-class │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── multi-line-comments │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── numbers-are-objects │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── single-line-comments │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── string-interpolation │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── use-named-parameters │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── function-as-parameter │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── function-omitting-types │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── method-then-constructor │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── op-increment-decrement │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── specify-default-values │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── adjacent-string-literals │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── reference │ │ ├── README │ │ ├── point_interfaces.dart │ │ ├── immutable_point.dart │ │ ├── constructor_long_way.dart │ │ ├── named_constructor.dart │ │ ├── along_x_axis.dart │ │ ├── distance_to.dart │ │ ├── doer.dart │ │ ├── initializer_list.dart │ │ ├── strictly_booleans.dart │ │ ├── rethrow.dart │ │ ├── smart_tv.dart │ │ ├── abstract.dart │ │ ├── doc_comments.dart │ │ └── map_literal.dart │ ├── specify-named-parameters │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── deferred │ │ ├── pubspec.yaml │ │ ├── lib │ │ │ └── hello.dart │ │ └── bin │ │ │ └── main.dart │ ├── optional-positional-parameters │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── list-map-default-function-parameters │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── optional-positional-parameter-default │ │ ├── pubspec.yaml │ │ └── bin │ │ │ └── main.dart │ ├── flow │ │ ├── README.md │ │ ├── exceptions │ │ │ ├── pubspec.yaml │ │ │ └── web │ │ │ │ └── main.dart │ │ ├── for-loops │ │ │ ├── pubspec.yaml │ │ │ └── web │ │ │ │ └── main.dart │ │ ├── break-continue │ │ │ ├── pubspec.yaml │ │ │ └── web │ │ │ │ └── main.dart │ │ └── assert_example │ │ │ ├── pubspec.yaml │ │ │ └── web │ │ │ └── main.dart │ ├── overrides │ │ ├── pubspec.yaml │ │ └── bin │ │ │ ├── override.dart │ │ │ ├── proxy.dart │ │ │ └── main.dart │ ├── cascade-operator │ │ ├── pubspec.yaml │ │ ├── web │ │ │ ├── main.dart │ │ │ └── index.html │ │ └── bin │ │ │ └── main.dart │ └── meta-overrides │ │ ├── pubspec.yaml │ │ └── bin │ │ ├── todo.dart │ │ └── main.dart └── .analysis_options ├── archive ├── figs │ └── web │ │ ├── run.png │ │ ├── newapp.png │ │ ├── callees.png │ │ ├── daur_0101.png │ │ ├── daur_0102.png │ │ ├── daur_0103.png │ │ ├── daur_0104.png │ │ ├── daur_0105.png │ │ ├── daur_0401.png │ │ ├── daur_0402.png │ │ ├── daur_0403.png │ │ ├── daur_0404.png │ │ ├── daur_0405.png │ │ ├── daur_0406.png │ │ ├── daur_0407.png │ │ ├── daur_0408.png │ │ ├── daur_0501.png │ │ ├── daur_0502.png │ │ ├── new-dart2js.png │ │ ├── Dart_Logo_21.png │ │ └── watchlist_view.gif ├── colo.xml ├── book.xml ├── foreword.xml ├── bookinfo.xml └── ch01.xml ├── .gitignore ├── README.md ├── LICENSE └── runtests.sh /code/library-tour/write-file/log.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /code/library-tour/write-file/betterLog.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /code/library-tour/write-file/copyOfIcon.ico: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /code/library-tour/get-data-from-server/web/nodata.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /code/library-tour/get-data-from-server/web/nodata.json: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /code/language-tour/libraries/lib1/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: lib1 2 | -------------------------------------------------------------------------------- /code/language-tour/libraries/lib2/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: lib2 2 | -------------------------------------------------------------------------------- /code/language-tour/libraries/mylib/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: mylib 2 | -------------------------------------------------------------------------------- /code/language-tour/libraries/utils/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: utils 2 | -------------------------------------------------------------------------------- /code/language-tour/args/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: args 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/color/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: color 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/const/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: const 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/libraries/mylib/lib/mylib.dart: -------------------------------------------------------------------------------- 1 | library mylib.dart; 2 | -------------------------------------------------------------------------------- /code/language-tour/libraries/using_libraries.dart: -------------------------------------------------------------------------------- 1 | import 'dart:html'; 2 | -------------------------------------------------------------------------------- /code/language-tour/libraries/utils/lib/utils.dart: -------------------------------------------------------------------------------- 1 | library utils.dart; 2 | -------------------------------------------------------------------------------- /code/language-tour/op-as/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: op_as 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/point/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: point 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/runes/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: runes 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/date/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: date 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/map-1/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: map_1 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/generics/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: generics 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/if-one/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: if_one 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/mixins/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: mixins 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/quoting/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: quoting 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/symbols/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: symbols 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/vector/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: vector 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/async-await/async-await-web/web/humans.txt: -------------------------------------------------------------------------------- 1 | We are not robots. -------------------------------------------------------------------------------- /code/library-tour/duration/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: duration 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/futures/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: futures 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/iterator/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: iterator 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/map-keys/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: map_keys 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/mirrors/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: mirrors 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/flow-while/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: flow_while 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/map-length/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: map_length 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/op-assign/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: op_assign 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/op-bitwise/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: op_bitwise 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/op-logical/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: op_logical 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/precedence/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: precedence 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/rectangle/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: rectangle 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/comparable/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: comparable 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/encode-uri/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: encode_uri 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/json-parse/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: json_parse 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/list-files/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: list_files 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/list-tests/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: list_tests 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/math-tests/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: math_tests 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/parse-uri/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: parse_uri 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/read-file/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: read_file 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/set-tests/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: set_tests 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/write-file/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: write_file 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/async-await/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: async_await 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/bit-shifting/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: bit_shifting 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/empty-string/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: empty_string 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/enum-switch/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: enum_switch 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/flow-if-else/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: flow_if_else 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/french-togo/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: french_togo 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/imposter/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: while_collection 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/list-indexing/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: list_indexing 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/list-literal/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: list_literal 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/map-add-item/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: map_add_item 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/op-equality/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: op_equality 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/raw-strings/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: raw_strings 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/static-types/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: static_types 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/triple-quotes/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: triple_quotes 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/client-server/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: http_client 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/decode-utf-8/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: decode_utf_8 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/encode-utf-8/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: encode-utf-8 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/number-tests/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: number_tests 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/string-tests/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: string_tests 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/ballgame/ball.dart: -------------------------------------------------------------------------------- 1 | part of ballgame; 2 | 3 | // ...Code goes here... 4 | -------------------------------------------------------------------------------- /code/language-tour/ballgame/util.dart: -------------------------------------------------------------------------------- 1 | part of ballgame; 2 | 3 | // ...Code goes here... 4 | -------------------------------------------------------------------------------- /code/language-tour/const-vs-const/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: const_vs_const 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/double-literals/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: double_literals 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/map-constructor/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: map_constructor 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/map-missing-key/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: map_missing_key 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/no-such-method/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: no_such_method 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/number-literals/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: number_literals 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/object-classes/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: object_classes 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/string-literals/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: string_literals 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/json-stringify/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: json_stringify 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/basic-dart-program/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: basic_dart_program 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/callable-function/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: callable_function 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/continue_example/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: continue_example 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/flow-switch-case/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: flow_switch_case 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/function-closure/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: function_closure 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/function-example/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: function-example 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/function-shorthand/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: function_shorthand 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/instance-variables/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: instance_variables 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/integer-literals/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: integer_literals 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/map-retrieve-item/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: map_retrieve_item 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/nested-functions/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: nested_functions 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/number-conversion/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: number_conversion 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/sorted-collection/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: sorted_collection 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/while-collection/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: while_collection 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/arithmetic-operators/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: arithmetic_operators 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/creating-a-variable/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: creating_a_variable 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/factory-constructor/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: factory_constructor 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/final-initialization/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: final_initialization 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/function-as-variable/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: function_as_variable 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/function-equality-2/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: function-equality-2 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/generics-base-class/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: generics_base_class 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/multi-line-comments/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: multi_line_comments 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/numbers-are-objects/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: numbers_are_objects 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/single-line-comments/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: single_line_comments 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/string-interpolation/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: string_interpolation 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/use-named-parameters/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: use_named_parameters 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/collection-any-every/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: collection_any_every 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/collection-is-empty/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: collection_is_empty 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/encode-uri-components/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: encode-uri-components 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/uri-from-components/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: uri_from_components 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/function-as-parameter/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: function_as_parameter 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/function-omitting-types/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: function_omitting_types 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/method-then-constructor/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: method_then_constructor 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/op-increment-decrement/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: op_increment_decrement 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/specify-default-values/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: specify_default_values 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/adjacent-string-literals/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: adjacent_string_literals 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/reference/README: -------------------------------------------------------------------------------- 1 | The examples in this directory don't run. They are for reference only. 2 | -------------------------------------------------------------------------------- /code/language-tour/specify-named-parameters/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: specify_named_parameters 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/library-tour/collection-apply-function/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: collection_apply_function 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/deferred/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: deferred 2 | description: A sample app that loads a library lazily. 3 | -------------------------------------------------------------------------------- /code/language-tour/french-togo/lib/togo.dart: -------------------------------------------------------------------------------- 1 | import 'src/french.dart'; 2 | export 'src/french.dart' show hello; 3 | -------------------------------------------------------------------------------- /code/library-tour/read-file/bin/config.txt: -------------------------------------------------------------------------------- 1 | Llamas 2 | Dromedaries 3 | Oases in the Desert 4 | And other oddities 5 | -------------------------------------------------------------------------------- /archive/figs/web/run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/run.png -------------------------------------------------------------------------------- /code/language-tour/optional-positional-parameters/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: optional_positional_parameters 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /archive/figs/web/newapp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/newapp.png -------------------------------------------------------------------------------- /archive/figs/web/callees.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/callees.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0101.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0101.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0102.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0102.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0103.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0103.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0104.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0104.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0105.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0105.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0401.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0401.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0402.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0402.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0403.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0403.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0404.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0404.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0405.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0405.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0406.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0406.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0407.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0407.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0408.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0408.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0501.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0501.png -------------------------------------------------------------------------------- /archive/figs/web/daur_0502.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/daur_0502.png -------------------------------------------------------------------------------- /code/language-tour/french-togo/lib/src/french.dart: -------------------------------------------------------------------------------- 1 | void hello() => print('Bonjour!'); 2 | void goodbye() => print('Au Revoir!'); 3 | -------------------------------------------------------------------------------- /archive/figs/web/new-dart2js.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/new-dart2js.png -------------------------------------------------------------------------------- /code/language-tour/list-map-default-function-parameters/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: list_map_default_function_parameters 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /code/language-tour/optional-positional-parameter-default/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: optional_positional_parameter_default 2 | version: 0.0.2 3 | -------------------------------------------------------------------------------- /archive/figs/web/Dart_Logo_21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/Dart_Logo_21.png -------------------------------------------------------------------------------- /archive/figs/web/watchlist_view.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/archive/figs/web/watchlist_view.gif -------------------------------------------------------------------------------- /code/library-tour/future-then/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: future_then 2 | description: A sample web application 3 | dependencies: 4 | browser: any 5 | -------------------------------------------------------------------------------- /code/language-tour/flow/README.md: -------------------------------------------------------------------------------- 1 | These files don't depend on dart:html. We're using web app conventions 2 | only so that we can generate gists. 3 | -------------------------------------------------------------------------------- /code/language-tour/libraries/using_schemes.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | import 'package:mylib/mylib.dart'; 3 | import 'package:utils/utils.dart'; 4 | -------------------------------------------------------------------------------- /code/language-tour/overrides/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: overrides 2 | description: A sample command-line application 3 | dependencies: 4 | meta: any 5 | -------------------------------------------------------------------------------- /code/language-tour/static-types/bin/main.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(static_types) 2 | String name = 'Bob'; 3 | // END(static_types) 4 | 5 | void main() {} 6 | -------------------------------------------------------------------------------- /code/language-tour/cascade-operator/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: cascade_operator 2 | description: A sample application 3 | dependencies: 4 | browser: any 5 | -------------------------------------------------------------------------------- /code/language-tour/overrides/bin/override.dart: -------------------------------------------------------------------------------- 1 | class A { 2 | @override 3 | void noSuchMethod(Invocation mirror) { 4 | // ... 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /code/language-tour/meta-overrides/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: meta_overrides 2 | description: A sample command-line application 3 | dependencies: 4 | meta: any 5 | -------------------------------------------------------------------------------- /code/library-tour/write-file/bin/icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-up-and-running-book/HEAD/code/library-tour/write-file/bin/icon.ico -------------------------------------------------------------------------------- /code/library-tour/async-await/async-await-web/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: async_await_web 2 | description: A sample web application 3 | dependencies: 4 | browser: any 5 | -------------------------------------------------------------------------------- /code/library-tour/post-data-to-server/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: post_data_to_server 2 | description: A sample web application 3 | dependencies: 4 | browser: any 5 | -------------------------------------------------------------------------------- /code/language-tour/creating-a-variable/bin/main.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(creating_a_variable) 2 | String name = 'Bob'; 3 | // END(creating_a_variable) 4 | 5 | void main() {} 6 | -------------------------------------------------------------------------------- /code/language-tour/deferred/lib/hello.dart: -------------------------------------------------------------------------------- 1 | library deferred.hello; 2 | 3 | void printGreeting() { 4 | print('Hello World, from the deferred library!'); 5 | } 6 | -------------------------------------------------------------------------------- /code/library-tour/get-data-from-server/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: getting_data_from_server 2 | description: A sample web application 3 | dependencies: 4 | browser: any 5 | -------------------------------------------------------------------------------- /code/language-tour/flow/exceptions/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: exceptions 2 | dependencies: 3 | browser: any 4 | 5 | homepage: http://gist.github.com/kwalrath/bb5c060b6d9d050419c8 -------------------------------------------------------------------------------- /code/language-tour/op-assign/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var a = 2; // Assign using = 3 | a *= 3; // Assign and multiply: a = a * 3 4 | assert(a == 6); 5 | } 6 | -------------------------------------------------------------------------------- /code/language-tour/flow/for-loops/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: for_loops 2 | dependencies: 3 | browser: any 4 | 5 | homepage: http://gist.github.com/kwalrath/836a6f67f4ef7af349bc 6 | -------------------------------------------------------------------------------- /code/language-tour/french-togo/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'package:french_togo/togo.dart'; 2 | 3 | void main() { 4 | hello(); //print bonjour 5 | /*goodbye();*/ //FAIL 6 | } 7 | -------------------------------------------------------------------------------- /code/language-tour/raw-strings/bin/main.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(raw_strings) 2 | String s = r"In a raw string, even \n isn't special."; 3 | // END(raw_strings) 4 | 5 | void main() {} 6 | -------------------------------------------------------------------------------- /code/language-tour/reference/point_interfaces.dart: -------------------------------------------------------------------------------- 1 | class Point implements Comparable, Location { 2 | // ... 3 | } 4 | 5 | class Comparable {} 6 | 7 | class Location {} 8 | -------------------------------------------------------------------------------- /code/library-tour/get-data-from-server/web/baddata.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dart-up-and-running", 3 | "home": "https://github.com/dart-lang/dart-up-and-running-book" 4 | } 5 | -------------------------------------------------------------------------------- /code/language-tour/double-literals/bin/main.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(double_literals) 2 | double y = 1.1; 3 | double exponents = 1.42e5; 4 | // END(double_literals) 5 | 6 | void main() {} 7 | -------------------------------------------------------------------------------- /code/language-tour/flow/break-continue/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: break_continue 2 | dependencies: 3 | browser: any 4 | 5 | homepage: http://gist.github.com/kwalrath/7417291c1fd6c74b56be -------------------------------------------------------------------------------- /code/language-tour/single-line-comments/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // TODO: refactor into an AbstractLlamaGreetingFactory? 3 | print('Welcome to my Llama farm!'); 4 | } 5 | -------------------------------------------------------------------------------- /code/language-tour/meta-overrides/bin/todo.dart: -------------------------------------------------------------------------------- 1 | library todo; 2 | 3 | class Todo { 4 | final String who; 5 | final String what; 6 | 7 | const Todo(this.who, this.what); 8 | } 9 | -------------------------------------------------------------------------------- /code/language-tour/op-logical/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var done = false; 3 | var col = 2; 4 | if (!done && (col == 0 || col == 3)) { 5 | // ...Do something... 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /code/library-tour/get-data-from-server/web/data.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dart-up-and-running", 3 | "license": "CC BY-NC-ND", 4 | "home": "https://github.com/dart-lang/dart-up-and-running-book" 5 | } 6 | -------------------------------------------------------------------------------- /code/language-tour/libraries/lib1/lib/lib1.dart: -------------------------------------------------------------------------------- 1 | library lib1.dart; 2 | 3 | void foo() { 4 | print('foo'); 5 | } 6 | 7 | void bar() { 8 | print('bar'); 9 | } 10 | 11 | class Element {} 12 | -------------------------------------------------------------------------------- /code/language-tour/libraries/lib2/lib/lib2.dart: -------------------------------------------------------------------------------- 1 | library lib2.dart; 2 | 3 | void foo() { 4 | print('FOO'); 5 | } 6 | 7 | void bar() { 8 | print('BAR'); 9 | } 10 | 11 | class Element {} 12 | -------------------------------------------------------------------------------- /code/language-tour/libraries/library_partial.dart: -------------------------------------------------------------------------------- 1 | // Import only foo. 2 | import 'package:lib1/lib1.dart' show foo; 3 | 4 | // Import all names EXCEPT foo. 5 | import 'package:lib2/lib2.dart' hide foo; 6 | -------------------------------------------------------------------------------- /code/language-tour/sorted-collection/bin/main.dart: -------------------------------------------------------------------------------- 1 | typedef int Compare(Object a, Object b); 2 | 3 | int sort(int a, int b) => a - b; 4 | 5 | void main() { 6 | assert(sort is Compare); // True! 7 | } 8 | -------------------------------------------------------------------------------- /code/language-tour/map-missing-key/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(map_missing_key) 3 | var gifts = {'first': 'partridge'}; 4 | assert(gifts['fifth'] == null); 5 | // END(map_missing_key) 6 | } 7 | -------------------------------------------------------------------------------- /code/language-tour/ballgame/ballgame.dart: -------------------------------------------------------------------------------- 1 | library ballgame; 2 | 3 | import 'dart:html'; 4 | // ...Other imports go here... 5 | 6 | part 'ball.dart'; 7 | part 'util.dart'; 8 | 9 | // ...Code might go here... 10 | -------------------------------------------------------------------------------- /code/language-tour/const/bin/main.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(const) 2 | const int bar = 1000000; // Unit of pressure (dynes/cm2) 3 | const double atm = 1.01325 * bar; // Standard atmosphere 4 | // END(const) 5 | 6 | void main() {} 7 | -------------------------------------------------------------------------------- /code/language-tour/function-as-variable/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var loudify = (msg) => '!!! ${msg.toUpperCase()} !!!'; 3 | assert(loudify('hello') == '!!! HELLO !!!'); 4 | print(loudify('hello')); 5 | } 6 | -------------------------------------------------------------------------------- /archive/colo.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /code/language-tour/map-retrieve-item/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(map_retrieve_item) 3 | var gifts = {'first': 'partridge'}; 4 | assert(gifts['first'] == 'partridge'); 5 | // END(map_retrieve_item) 6 | } 7 | -------------------------------------------------------------------------------- /code/language-tour/map-add-item/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(map_add_item) 3 | var gifts = {'first': 'partridge'}; 4 | gifts['fourth'] = 'calling birds'; // Add a key-value pair 5 | // END(map_add_item) 6 | } 7 | -------------------------------------------------------------------------------- /code/language-tour/libraries/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: dart_up_and_running_samples 2 | dependencies: 3 | lib1: 4 | path: lib1 5 | lib2: 6 | path: lib2 7 | mylib: 8 | path: mylib 9 | utils: 10 | path: utils 11 | -------------------------------------------------------------------------------- /code/language-tour/map-length/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(map_length) 3 | var gifts = {'first': 'partridge'}; 4 | gifts['fourth'] = 'calling birds'; 5 | assert(gifts.length == 2); 6 | // END(map_length) 7 | } 8 | -------------------------------------------------------------------------------- /code/language-tour/op-equality/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | //BEGIN 3 | assert(2 == 2); 4 | assert(2 != 3); 5 | assert(3 > 2); 6 | assert(2 < 3); 7 | assert(3 >= 3); 8 | assert(2 <= 3); 9 | //END 10 | } 11 | -------------------------------------------------------------------------------- /code/language-tour/reference/immutable_point.dart: -------------------------------------------------------------------------------- 1 | class ImmutablePoint { 2 | final num x; 3 | final num y; 4 | const ImmutablePoint(this.x, this.y); 5 | static final ImmutablePoint origin = const ImmutablePoint(0, 0); 6 | } 7 | -------------------------------------------------------------------------------- /code/library-tour/collection-is-empty/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(collection-isEmpty) 3 | var teas = ['green', 'black', 'chamomile', 'earl grey']; 4 | assert(teas.isNotEmpty); 5 | // END(collection-isEmpty) 6 | } 7 | -------------------------------------------------------------------------------- /code/language-tour/flow/assert_example/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: assert_example 2 | dependencies: 3 | browser: any 4 | 5 | gist: https://gist.github.com/kwalrath/dab00eb68704900f70ba 6 | dartpad: https://dartpad.dartlang.org/dab00eb68704900f70ba 7 | -------------------------------------------------------------------------------- /code/language-tour/reference/constructor_long_way.dart: -------------------------------------------------------------------------------- 1 | class Point { 2 | num x; 3 | num y; 4 | 5 | Point(num x, num y) { 6 | // There's a better way to do this, stay tuned. 7 | this.x = x; 8 | this.y = y; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /code/language-tour/use-named-parameters/bin/main.dart: -------------------------------------------------------------------------------- 1 | void enableFlags({bool bold, bool hidden}) {} 2 | 3 | void main() { 4 | // BEGIN(use_named_parameters) 5 | enableFlags(bold: true, hidden: false); 6 | // END(use_named_parameters) 7 | } 8 | -------------------------------------------------------------------------------- /code/library-tour/uri-from-components/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var uri = new Uri( 3 | scheme: 'http', host: 'example.org', path: '/foo/bar', fragment: 'frag'); 4 | assert(uri.toString() == 'http://example.org/foo/bar#frag'); 5 | } 6 | -------------------------------------------------------------------------------- /code/language-tour/multi-line-comments/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | /* 3 | * This is a lot of work. Consider raising chickens. 4 | 5 | Llama larry = new Llama(); 6 | larry.feed(); 7 | larry.exercise(); 8 | larry.clean(); 9 | */ 10 | } 11 | -------------------------------------------------------------------------------- /code/language-tour/reference/named_constructor.dart: -------------------------------------------------------------------------------- 1 | class Point { 2 | num x; 3 | num y; 4 | 5 | Point(this.x, this.y); 6 | 7 | // Named constructor 8 | Point.fromJson(Map json) { 9 | x = json['x']; 10 | y = json['y']; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /code/library-tour/get-data-from-server/web/data.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | dart-up-and-running 4 | CC BY-NC-ND 5 | https://github.com/dart-lang/dart-up-and-running-book 6 | 7 | -------------------------------------------------------------------------------- /code/language-tour/function-as-parameter/bin/main.dart: -------------------------------------------------------------------------------- 1 | void printElement(int element) { 2 | print(element); 3 | } 4 | 5 | void main() { 6 | var list = [1, 2, 3]; 7 | 8 | // Pass printElement as a parameter. 9 | list.forEach(printElement); 10 | } 11 | -------------------------------------------------------------------------------- /code/language-tour/numbers-are-objects/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(numbers_are_objects) 3 | int lineCount; 4 | assert(lineCount == null); 5 | // Variables (even if they will be numbers) are initially null. 6 | // END(numbers_are_objects) 7 | } 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /code/ch03/ch03_2_html/packages/browser 3 | packages 4 | /code/ch03/ch03_2_html/web/packages 5 | out 6 | /code/ch03/log.txt 7 | /code/ch03/betterLog.txt 8 | /code/ch03/copyOfIcon.ico 9 | pubspec.lock 10 | .idea 11 | .packages 12 | .pub 13 | -------------------------------------------------------------------------------- /code/language-tour/bit-shifting/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(bit_shifting) 3 | assert((3 << 1) == 6); // 0011 << 1 == 0110 4 | assert((3 >> 1) == 1); // 0011 >> 1 == 0001 5 | assert((3 | 4) == 7); // 0011 | 0100 == 0111 6 | // END(bit_shifting) 7 | } 8 | -------------------------------------------------------------------------------- /code/language-tour/libraries/library_prefix.dart: -------------------------------------------------------------------------------- 1 | import 'package:lib1/lib1.dart'; 2 | import 'package:lib2/lib2.dart' as lib2; 3 | 4 | Element element1 = new Element(); // Uses Element from lib1. 5 | lib2.Element element2 = new lib2.Element(); // Uses Element from lib2. 6 | -------------------------------------------------------------------------------- /code/language-tour/args/bin/main.dart: -------------------------------------------------------------------------------- 1 | // Run the app like this: dart args.dart 1 test 2 | void main(List arguments) { 3 | print(arguments); 4 | 5 | assert(arguments.length == 2); 6 | assert(int.parse(arguments[0]) == 1); 7 | assert(arguments[1] == 'test'); 8 | } 9 | -------------------------------------------------------------------------------- /code/language-tour/list-indexing/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(list_indexing) 3 | var list = [1, 2, 3]; 4 | assert(list.length == 3); 5 | assert(list[1] == 2); 6 | 7 | list[1] = 1; 8 | assert(list[1] == 1); 9 | // END(list_indexing) 10 | } 11 | -------------------------------------------------------------------------------- /code/language-tour/reference/along_x_axis.dart: -------------------------------------------------------------------------------- 1 | class Point { 2 | num x; 3 | num y; 4 | 5 | // The main constructor for this class. 6 | Point(this.x, this.y); 7 | 8 | // Delegates to the main constructor. 9 | Point.alongXAxis(num x) : this(x, 0); 10 | } 11 | -------------------------------------------------------------------------------- /code/language-tour/triple-quotes/bin/main.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(triple_quotes) 2 | String s1 = ''' 3 | You can create 4 | multi-line strings like this one. 5 | '''; 6 | 7 | String s2 = """This is also a 8 | multi-line string."""; 9 | // END(triple_quotes) 10 | 11 | void main() {} 12 | -------------------------------------------------------------------------------- /code/library-tour/reference/exceptions.dart: -------------------------------------------------------------------------------- 1 | main() { 2 | throw new FooException(); 3 | } 4 | 5 | class FooException implements Exception { 6 | final String msg; 7 | const FooException([this.msg]); 8 | String toString() => msg ?? 'FooException (no message)'; 9 | } 10 | -------------------------------------------------------------------------------- /code/language-tour/overrides/bin/proxy.dart: -------------------------------------------------------------------------------- 1 | @proxy 2 | class A { 3 | @override 4 | void noSuchMethod(Invocation mirror) { 5 | print('handling invocation: ${mirror.memberName}'); 6 | } 7 | } 8 | 9 | void main() { 10 | var a = new A(); 11 | a.doSomething(); 12 | } 13 | -------------------------------------------------------------------------------- /code/language-tour/cascade-operator/web/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:html'; 2 | 3 | void main() { 4 | querySelector('#button') // Get an object. 5 | ..text = 'Confirm' // Use its members. 6 | ..classes.add('important') 7 | ..onClick.listen((e) => window.alert('Confirmed!')); 8 | } 9 | -------------------------------------------------------------------------------- /code/library-tour/get-data-from-server/web/styles.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | background-color: #F8F8F8; 4 | font-family: 'Open Sans', sans-serif; 5 | font-size: 14px; 6 | font-weight: normal; 7 | line-height: 1.2em; 8 | margin: 15px; 9 | } 10 | 11 | h1, p { 12 | color: #333; 13 | } -------------------------------------------------------------------------------- /code/library-tour/post-data-to-server/web/styles.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | background-color: #F8F8F8; 4 | font-family: 'Open Sans', sans-serif; 5 | font-size: 14px; 6 | font-weight: normal; 7 | line-height: 1.2em; 8 | margin: 15px; 9 | } 10 | 11 | h1, p { 12 | color: #333; 13 | } -------------------------------------------------------------------------------- /code/language-tour/integer-literals/bin/main.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(integer_literals) 2 | int x = 1; 3 | int hex = 0xDEADBEEF; 4 | int bigInt = 34653465834652437659238476592374958739845729; 5 | // END(integer_literals) 6 | 7 | void main() { 8 | print('x: $x; hex: $hex; bigInt: $bigInt'); 9 | } 10 | -------------------------------------------------------------------------------- /code/language-tour/callable-function/bin/main.dart: -------------------------------------------------------------------------------- 1 | class WannabeFunction { 2 | void call(String a, String b, String c) => a + ' ' + b + ' ' + c + '!'; 3 | } 4 | 5 | void main() { 6 | var wf = new WannabeFunction(); 7 | var out = wf("Hi", "there,", "gang"); 8 | print('$out'); 9 | } 10 | -------------------------------------------------------------------------------- /code/language-tour/precedence/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var n = 4; 3 | var d = 2; 4 | var i = 2; 5 | 6 | // 1: Parens improve readability. 7 | if ((n % i == 0) && (d % i == 0)) {} 8 | 9 | // 2: Harder to read, but equivalent. 10 | if (n % i == 0 && d % i == 0) {} 11 | } 12 | -------------------------------------------------------------------------------- /code/library-tour/read-file/bin/binary_read.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | import 'dart:io'; 3 | 4 | Future main() async { 5 | var config = new File('config.txt'); 6 | 7 | var contents = await config.readAsBytes(); 8 | print('The entire file is ${contents.length} bytes long'); 9 | } 10 | -------------------------------------------------------------------------------- /code/language-tour/number-literals/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(number_literals) 3 | const msPerSecond = 1000; 4 | const secondsUntilRetry = 5; 5 | const msUntilRetry = secondsUntilRetry * msPerSecond; 6 | // END(number_literals) 7 | print('Wait $msUntilRetry milliseconds.'); 8 | } 9 | -------------------------------------------------------------------------------- /code/language-tour/reference/distance_to.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | class Point { 4 | num x; 5 | num y; 6 | Point(this.x, this.y); 7 | 8 | num distanceTo(Point other) { 9 | var dx = x - other.x; 10 | var dy = y - other.y; 11 | return sqrt(dx * dx + dy * dy); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /code/library-tour/read-file/bin/file_errors.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | import 'dart:async'; 3 | 4 | Future main() async { 5 | var config = new File('config.txt'); 6 | try { 7 | var contents = await config.readAsString(); 8 | print(contents); 9 | } catch (e) { 10 | print(e); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /code/language-tour/specify-named-parameters/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(specify_named_parameters) 3 | /// Sets the [bold] and [hidden] flags to the values 4 | /// you specify. 5 | void enableFlags({bool bold, bool hidden}) { 6 | //... 7 | } 8 | // END(specify_named_parameters) 9 | } 10 | -------------------------------------------------------------------------------- /code/language-tour/color/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Color { 2 | static const Color red = const Color('red'); // A constant static variable. 3 | final String name; // An instance variable. 4 | const Color(this.name); // A constant constructor. 5 | } 6 | 7 | void main() { 8 | assert(Color.red.name == 'red'); 9 | } 10 | -------------------------------------------------------------------------------- /code/library-tour/encode-uri/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var uri = 'http://example.org/api?foo=some message'; 3 | 4 | var encoded = Uri.encodeFull(uri); 5 | assert(encoded == 'http://example.org/api?foo=some%20message'); 6 | 7 | var decoded = Uri.decodeFull(encoded); 8 | assert(uri == decoded); 9 | } 10 | -------------------------------------------------------------------------------- /code/language-tour/flow-while/bin/main.dart: -------------------------------------------------------------------------------- 1 | void doSomething() {} 2 | void printLine() => print('Hi'); 3 | bool isDone() => true; 4 | bool atEndOfPage() => true; 5 | 6 | void main() { 7 | while (!isDone()) { 8 | doSomething(); 9 | } 10 | 11 | do { 12 | printLine(); 13 | } while (!atEndOfPage()); 14 | } 15 | -------------------------------------------------------------------------------- /code/library-tour/parse-uri/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var uri = Uri.parse('http://example.org:8080/foo/bar#frag'); 3 | 4 | assert(uri.scheme == 'http'); 5 | assert(uri.host == 'example.org'); 6 | assert(uri.path == '/foo/bar'); 7 | assert(uri.fragment == 'frag'); 8 | assert(uri.origin == 'http://example.org:8080'); 9 | } 10 | -------------------------------------------------------------------------------- /code/language-tour/reference/doer.dart: -------------------------------------------------------------------------------- 1 | abstract class Doer { 2 | // ...Define instance variables and methods... 3 | 4 | void doSomething(); // Define an abstract method. 5 | } 6 | 7 | class EffectiveDoer extends Doer { 8 | void doSomething() { 9 | // ...Provide an implementation, so the method is not abstract here... 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /code/language-tour/arithmetic-operators/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | assert(2 + 3 == 5); 3 | assert(2 - 3 == -1); 4 | assert(2 * 3 == 6); 5 | assert(5 / 2 == 2.5); // Result is a double 6 | assert(5 ~/ 2 == 2); // Result is an integer 7 | assert(5 % 2 == 1); // Remainder 8 | 9 | print('5/2 = ${5~/2} r ${5%2}'); // 5/2 = 2 r 1 10 | } 11 | -------------------------------------------------------------------------------- /code/library-tour/encode-uri-components/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var uri = 'http://example.org/api?foo=some message'; 3 | 4 | var encoded = Uri.encodeComponent(uri); 5 | assert(encoded == 'http%3A%2F%2Fexample.org%2Fapi%3Ffoo%3Dsome%20message'); 6 | 7 | var decoded = Uri.decodeComponent(encoded); 8 | assert(uri == decoded); 9 | } 10 | -------------------------------------------------------------------------------- /code/library-tour/comparable/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Line implements Comparable { 2 | final int length; 3 | const Line(this.length); 4 | @override 5 | int compareTo(Line other) => length - other.length; 6 | } 7 | 8 | void main() { 9 | var short = const Line(1); 10 | var long = const Line(100); 11 | assert(short.compareTo(long) < 0); 12 | } 13 | -------------------------------------------------------------------------------- /code/language-tour/meta-overrides/bin/main.dart: -------------------------------------------------------------------------------- 1 | //import 'dart:mirrors'; 2 | // BEGIN 3 | import 'todo.dart'; 4 | 5 | @Todo('seth', 'make this do something') 6 | void doSomething() { 7 | print('do something'); 8 | } 9 | // END 10 | 11 | void main() { 12 | // PENDING: Once reflection on annotations is implemented, show that here. 13 | print('tbd'); 14 | } 15 | -------------------------------------------------------------------------------- /code/language-tour/reference/initializer_list.dart: -------------------------------------------------------------------------------- 1 | class Point { 2 | num x; 3 | num y; 4 | 5 | Point(this.x, this.y); 6 | 7 | // Initializer list sets instance variables before 8 | // the constructor body runs. 9 | Point.fromJson(Map json) 10 | : x = json['x'], 11 | y = json['y'] { 12 | print('In Point.fromJson(): ($x, $y)'); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /code/language-tour/reference/strictly_booleans.dart: -------------------------------------------------------------------------------- 1 | main() { 2 | /* HACK: This code is fails the type checker, so I don't want to actually run it. 3 | // BEGIN(strictly_booleans) 4 | var name = 'Bob'; 5 | if (name) { 6 | // Prints in JavaScript, not in Dart. 7 | print('You have a name!'); 8 | } 9 | // END(strictly_booleans) 10 | */ 11 | } 12 | -------------------------------------------------------------------------------- /code/language-tour/overrides/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Television { 2 | /// _Deprecated: Use [turnOn] instead._ 3 | @deprecated 4 | void activate() { 5 | turnOn(); 6 | } 7 | 8 | /// Turns the TV's power on. 9 | void turnOn() { 10 | print('on!'); 11 | } 12 | } 13 | 14 | void main() { 15 | var tv = new Television(); 16 | //tv.activate(); 17 | tv.turnOn(); 18 | } 19 | -------------------------------------------------------------------------------- /code/library-tour/reference/spawnIsolate.dart: -------------------------------------------------------------------------------- 1 | //import 'dart:isolate'; 2 | // 3 | //runInIsolate() { 4 | // print('hello from an isolate!'); 5 | //} 6 | // 7 | //main() { 8 | // spawnFunction(runInIsolate); 9 | // 10 | // // Note: incomplete. 11 | // // Use a ReceivePort (details below) to keep the root isolate alive 12 | // // long enough for runInIsolate() to perform its work. 13 | //} 14 | -------------------------------------------------------------------------------- /code/language-tour/if-one/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // Change if (true) to if (1) to really test this code. 3 | if (true) { 4 | // BEGIN(if_one) 5 | print('JS prints this line.'); 6 | } else { 7 | print('Dart in production mode prints this line.'); 8 | // However, in checked mode, if (1) throws an 9 | // exception because 1 is not boolean. 10 | } 11 | // END(if_one) 12 | } 13 | -------------------------------------------------------------------------------- /code/language-tour/map-constructor/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(map_constructor) 3 | var gifts = new Map(); 4 | gifts['first'] = 'partridge'; 5 | gifts['second'] = 'turtledoves'; 6 | gifts['fifth'] = 'golden rings'; 7 | 8 | var nobleGases = new Map(); 9 | nobleGases[2] = 'helium'; 10 | nobleGases[10] = 'neon'; 11 | nobleGases[18] = 'argon'; 12 | // END(map_constructor) 13 | } 14 | -------------------------------------------------------------------------------- /code/language-tour/deferred/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | import 'package:deferred/hello.dart' deferred as hello; 3 | 4 | Future greet() async { 5 | await hello.loadLibrary(); 6 | hello.printGreeting(); 7 | } 8 | 9 | Future main() async { 10 | await hello.loadLibrary(); 11 | hello.printGreeting(); 12 | // OR... 13 | greet(); 14 | greet(); 15 | greet(); 16 | greet(); 17 | greet(); 18 | } 19 | -------------------------------------------------------------------------------- /code/language-tour/op-bitwise/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | final value = 0x22; 3 | final bitmask = 0x0f; 4 | 5 | assert((value & bitmask) == 0x02); // AND 6 | assert((value & ~bitmask) == 0x20); // AND NOT 7 | assert((value | bitmask) == 0x2f); // OR 8 | assert((value ^ bitmask) == 0x2d); // XOR 9 | assert((value << 4) == 0x220); // Shift left 10 | assert((value >> 4) == 0x02); // Shift right 11 | } 12 | -------------------------------------------------------------------------------- /code/language-tour/basic-dart-program/bin/main.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(basic_dart_program) 2 | // Define a function. 3 | void printNumber(num aNumber) { 4 | print('The number is $aNumber.'); // Print to console. 5 | } 6 | 7 | // This is where the app starts executing. 8 | void main() { 9 | var number = 42; // Declare and initialize a variable. 10 | printNumber(number); // Call a function. 11 | } 12 | // END(basic_dart_program) 13 | -------------------------------------------------------------------------------- /code/language-tour/runes/bin/main.dart: -------------------------------------------------------------------------------- 1 | // Unicode emoji: http://unicode.org/emoji/charts/full-emoji-list.html 2 | 3 | void main() { 4 | var clapping = '\u{1f44f}'; 5 | print(clapping); 6 | print(clapping.codeUnits); 7 | print(clapping.runes.toList()); 8 | 9 | Runes input = new Runes( 10 | '\u2665 \u{1f605} \u{1f60e} \u{1f47b} \u{1f596} \u{1f44d}'); 11 | print(new String.fromCharCodes(input)); 12 | } 13 | -------------------------------------------------------------------------------- /code/language-tour/specify-default-values/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(specify_default_values) 3 | /// Sets the [bold] and [hidden] flags to the values you 4 | /// specify, defaulting to false. 5 | void enableFlags({bool bold = false, bool hidden = false}) { 6 | // ... 7 | } 8 | 9 | // bold will be true; hidden will be false. 10 | enableFlags(bold: true); 11 | // END(specify_default_values) 12 | } 13 | -------------------------------------------------------------------------------- /code/language-tour/nested-functions/bin/main.dart: -------------------------------------------------------------------------------- 1 | bool topLevel = true; 2 | 3 | void main() { 4 | var insideMain = true; 5 | 6 | void myFunction() { 7 | var insideFunction = true; 8 | 9 | void nestedFunction() { 10 | var insideNestedFunction = true; 11 | 12 | assert(topLevel); 13 | assert(insideMain); 14 | assert(insideFunction); 15 | assert(insideNestedFunction); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /code/language-tour/quoting/bin/main.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(quoting) 2 | String s1 = 'Single quotes work well for string literals.'; 3 | String s2 = "Double quotes work just as well."; 4 | String s3 = 'It\'s easy to escape the string delimiter.'; 5 | String s4 = "It's even easier to use the other delimiter."; 6 | // END(quoting) 7 | 8 | void main() { 9 | // Unicode escapes work: [heart] 10 | print('Unicode escapes work: \u2665'); 11 | } 12 | -------------------------------------------------------------------------------- /code/language-tour/no-such-method/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var a = new A(); 3 | //a.bar(); 4 | //a.baz; 5 | } 6 | 7 | //BEGIN 8 | class A { 9 | // Unless you override noSuchMethod, using a 10 | // non-existent member results in a NoSuchMethodError. 11 | @override 12 | void noSuchMethod(Invocation mirror) { 13 | print('You tried to use a non-existent member:' + '${mirror.memberName}'); 14 | } 15 | } 16 | //END 17 | -------------------------------------------------------------------------------- /code/language-tour/symbols/bin/main.dart: -------------------------------------------------------------------------------- 1 | // MOVE TO library tour? 2 | 3 | void main() { 4 | print(Function.apply(int.parse, ['11'])); 5 | print(Function.apply(int.parse, ['11'], {#radix: 16})); 6 | print(Function.apply(int.parse, ['11a'], {#onError: handleError})); 7 | print( 8 | Function.apply(int.parse, ['11a'], {#radix: 16, #onError: handleError})); 9 | } 10 | 11 | int handleError(String source) { 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /code/library-tour/json-stringify/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:convert' show JSON; 2 | 3 | void main() { 4 | var scores = [ 5 | {'score': 40}, 6 | {'score': 80}, 7 | {'score': 100, 'overtime': true, 'special_guest': null} 8 | ]; 9 | 10 | var jsonText = JSON.encode(scores); 11 | assert(jsonText == 12 | '[{"score":40},{"score":80},' 13 | '{"score":100,"overtime":true,' 14 | '"special_guest":null}]'); 15 | } 16 | -------------------------------------------------------------------------------- /code/language-tour/instance-variables/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Point { 2 | num x; // Declare instance variable x, initially null. 3 | num y; // Declare y, initially null. 4 | num z = 0; // Declare z, initially 0. 5 | } 6 | 7 | void main() { 8 | var point = new Point(); 9 | point.x = 4; // Use the setter method for x. 10 | assert(point.x == 4); // Use the getter method for x. 11 | assert(point.y == null); // Values default to null. 12 | } 13 | -------------------------------------------------------------------------------- /code/library-tour/port-send-receive/port_send.dart: -------------------------------------------------------------------------------- 1 | //import 'dart:isolate'; 2 | // 3 | //echo() { 4 | // // Receive messages here. (See the next section.) 5 | //} 6 | // 7 | //main() { 8 | // var sendPort = spawnFunction(echo); 9 | // sendPort.send('Hello from main'); 10 | // 11 | // // Note: incomplete. 12 | // // Use a ReceivePort (details below) to keep the root isolate alive 13 | // // long enough for echo() to perform its work. 14 | //} 15 | -------------------------------------------------------------------------------- /code/library-tour/collection-apply-function/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var teas = ['green', 'black', 'chamomile', 'earl grey']; 3 | 4 | teas.forEach((tea) => print('I drink $tea')); 5 | 6 | var loudTeas = teas.map((tea) => tea.toUpperCase()); 7 | assert(loudTeas.any((tea) => tea == 'GREEN')); 8 | loudTeas.forEach(print); 9 | 10 | var loudTeaList = teas.map((tea) => tea.toUpperCase()).toList(); 11 | loudTeaList.forEach(print); 12 | } 13 | -------------------------------------------------------------------------------- /code/language-tour/reference/rethrow.dart: -------------------------------------------------------------------------------- 1 | final foo = ''; 2 | 3 | void misbehave() { 4 | try { 5 | foo = "You can't change a final variable's value."; 6 | } catch (e) { 7 | print('misbehave() partially handled ${e.runtimeType}.'); 8 | rethrow; // Allow callers to see the exception. 9 | } 10 | } 11 | 12 | void main() { 13 | try { 14 | misbehave(); 15 | } catch (e) { 16 | print('main() finished handling ${e.runtimeType}.'); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /code/language-tour/string-interpolation/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(string_interpolation) 3 | var s = 'string interpolation'; 4 | 5 | assert('Dart has $s, which is very handy.' == 6 | 'Dart has string interpolation, ' + 'which is very handy.'); 7 | assert('That deserves all caps. ' + '${s.toUpperCase()} is very handy!' == 8 | 'That deserves all caps. ' + 'STRING INTERPOLATION is very handy!'); 9 | // END(string_interpolation) 10 | } 11 | -------------------------------------------------------------------------------- /code/language-tour/sorted-collection/bin/sorted_collection_broken_2.dart: -------------------------------------------------------------------------------- 1 | typedef int Compare(Object a, Object b); 2 | 3 | class SortedCollection { 4 | Compare compare; 5 | 6 | SortedCollection(this.compare); 7 | } 8 | 9 | // Initial, broken implementation. 10 | int sort(Object a, Object b) => 0; 11 | 12 | void main() { 13 | SortedCollection coll = new SortedCollection(sort); 14 | assert(coll.compare is Function); 15 | assert(coll.compare is Compare); 16 | } 17 | -------------------------------------------------------------------------------- /code/library-tour/port-send-receive/port_receive.dart: -------------------------------------------------------------------------------- 1 | //import 'dart:isolate'; 2 | // 3 | //echo() { 4 | // port.receive((msg, reply) { 5 | // print('I received: $msg'); 6 | // }); 7 | //} 8 | // 9 | //main() { 10 | // var sendPort = spawnFunction(echo); 11 | // sendPort.send('Hello from main'); 12 | // 13 | // // Note: incomplete. 14 | // // Use a ReceivePort (details below) to keep the root isolate alive 15 | // // long enough for echo() to perform its work. 16 | //} 17 | -------------------------------------------------------------------------------- /code/language-tour/empty-string/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(empty_string) 3 | // Check for an empty string. 4 | var fullName = ''; 5 | assert(fullName.isEmpty); 6 | 7 | // Check for zero. 8 | var hitPoints = 0; 9 | assert(hitPoints <= 0); 10 | 11 | // Check for null. 12 | var unicorn; 13 | assert(unicorn == null); 14 | 15 | // Check for NaN. 16 | var iMeantToDoThis = 0 / 0; 17 | assert(iMeantToDoThis.isNaN); 18 | // END(empty_string) 19 | } 20 | -------------------------------------------------------------------------------- /code/language-tour/point/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | class Point { 4 | num x; 5 | num y; 6 | Point(this.x, this.y); 7 | 8 | static num distanceBetween(Point a, Point b) { 9 | var dx = a.x - b.x; 10 | var dy = a.y - b.y; 11 | return sqrt(dx * dx + dy * dy); 12 | } 13 | } 14 | 15 | void main() { 16 | var a = new Point(2, 2); 17 | var b = new Point(4, 4); 18 | var distance = Point.distanceBetween(a, b); 19 | assert(distance < 2.9 && distance > 2.8); 20 | } 21 | -------------------------------------------------------------------------------- /code/library-tour/async-await/async-await-web/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | async-await 7 | 8 | 9 | 10 |

Ch03 4 async

11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /code/library-tour/list-files/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | import 'dart:io'; 3 | 4 | Future main() async { 5 | var dir = new Directory('/tmp'); 6 | 7 | try { 8 | var dirList = dir.list(); 9 | await for (FileSystemEntity f in dirList) { 10 | if (f is File) { 11 | print('Found file ${f.path}'); 12 | } else if (f is Directory) { 13 | print('Found dir ${f.path}'); 14 | } 15 | } 16 | } catch (e) { 17 | print(e.toString()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /code/language-tour/function-closure/bin/main.dart: -------------------------------------------------------------------------------- 1 | /// Returns a function that adds [addBy] to the 2 | /// function's argument. 3 | Function makeAdder(num addBy) { 4 | return (num i) => addBy + i; 5 | } 6 | 7 | void main() { 8 | // Create a function that adds 2. 9 | var add2 = makeAdder(2); 10 | 11 | // Create a function that adds 4. 12 | var add4 = makeAdder(4); 13 | 14 | assert(add2(3) == 5); 15 | assert(add4(3) == 7); 16 | 17 | print(add2(3)); // 5 18 | print(add4(3)); // 7 19 | } 20 | -------------------------------------------------------------------------------- /code/language-tour/list-literal/bin/main.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(list_literal) 2 | List list = [1, 2, 3]; 3 | // END(list_literal) 4 | 5 | void main() { 6 | // BEGIN(list_const_literal) 7 | var constantList = const [1, 2, 3]; 8 | // constantList[1] = 1; // Uncommenting this causes an error. 9 | // END(list_const_literal) 10 | 11 | list[1] = 1; // You can do this. 12 | try { 13 | constantList[1] = 1; // You can't do this. 14 | } catch (e) { 15 | print('Tsk tsk! $e'); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /code/language-tour/sorted-collection/bin/sorted_collection_broken.dart: -------------------------------------------------------------------------------- 1 | class SortedCollection { 2 | Function compare; 3 | 4 | SortedCollection(int f(Object a, Object b)) { 5 | compare = f; 6 | } 7 | } 8 | 9 | // Initial, broken implementation. 10 | int sort(Object a, Object b) => 0; 11 | 12 | void main() { 13 | SortedCollection coll = new SortedCollection(sort); 14 | 15 | // All we know is that compare is a function, 16 | // but what type of function? 17 | assert(coll.compare is Function); 18 | } 19 | -------------------------------------------------------------------------------- /code/language-tour/optional-positional-parameter-default/bin/main.dart: -------------------------------------------------------------------------------- 1 | String say(String from, String msg, 2 | [String device = 'carrier pigeon', String mood]) { 3 | var result = '$from says $msg'; 4 | if (device != null) { 5 | result = '$result with a $device'; 6 | } 7 | if (mood != null) { 8 | result = '$result (in a $mood mood)'; 9 | } 10 | return result; 11 | } 12 | 13 | void main() { 14 | print(say('Bob', 'Howdy')); 15 | assert(say('Bob', 'Howdy') == 'Bob says Howdy with a carrier pigeon'); 16 | } 17 | -------------------------------------------------------------------------------- /code/language-tour/optional-positional-parameters/bin/main.dart: -------------------------------------------------------------------------------- 1 | String say(String from, String msg, [String device]) { 2 | var result = '$from says $msg'; 3 | if (device != null) { 4 | result = '$result with a $device'; 5 | } 6 | return result; 7 | } 8 | 9 | void main() { 10 | assert(say('Bob', 'Howdy') == 'Bob says Howdy'); 11 | print(say('Bob', 'Howdy')); 12 | assert(say('Bob', 'Howdy', 'smoke signal') == 13 | 'Bob says Howdy with a smoke signal'); 14 | print(say('Bob', 'Howdy', 'smoke signal')); 15 | } 16 | -------------------------------------------------------------------------------- /code/library-tour/read-file/bin/text_read.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | import 'dart:io'; 3 | 4 | Future main() async { 5 | var config = new File('config.txt'); 6 | var contents; 7 | 8 | // Put the whole file in a single string. 9 | contents = await config.readAsString(); 10 | print('The entire file is ${contents.length} characters long.'); 11 | 12 | // Put each line of the file into its own string. 13 | contents = await config.readAsLines(); 14 | print('The entire file is ${contents.length} lines long.'); 15 | } 16 | -------------------------------------------------------------------------------- /code/library-tour/post-data-to-server/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | HTML testing from Chapter 3 7 | 8 | 9 | 10 |

HTML testing from Chapter 3

11 | 12 |

Nothing to see here. Look in the console for output.

13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /code/language-tour/method-then-constructor/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Person { 2 | Map data; 3 | Person.fromJson(this.data); 4 | } 5 | 6 | class Employee extends Person { 7 | Employee() : super.fromJson(findDefaultData()); 8 | Employee.fromJson(Map data) : super.fromJson(data); 9 | 10 | static Map findDefaultData() => {'employeeData': 'none'}; 11 | } 12 | 13 | void main() { 14 | var emp = new Employee.fromJson({}); 15 | print(emp.data); // {} 16 | 17 | emp = new Employee(); // {employeeData: none} 18 | print(emp.data); 19 | } 20 | -------------------------------------------------------------------------------- /code/language-tour/reference/smart_tv.dart: -------------------------------------------------------------------------------- 1 | class Television { 2 | void turnOn() { 3 | _illuminateDisplay(); 4 | _activateIrSensor(); 5 | } 6 | 7 | void _illuminateDisplay() {} 8 | void _activateIrSensor() {} 9 | } 10 | 11 | class SmartTelevision extends Television { 12 | void turnOn() { 13 | super.turnOn(); 14 | _bootNetworkInterface(); 15 | _initializeMemory(); 16 | _upgradeApps(); 17 | } 18 | 19 | void _bootNetworkInterface() {} 20 | void _initializeMemory() {} 21 | void _upgradeApps() {} 22 | } 23 | -------------------------------------------------------------------------------- /code/language-tour/function-shorthand/bin/main.dart: -------------------------------------------------------------------------------- 1 | var _nobleGases = { 2 | // Keys Values 3 | 2: 'helium', 4 | 10: 'neon', 5 | 18: 'argon', 6 | 36: 'krypton', 7 | 54: 'xenon', 8 | 86: 'radon', 9 | }; 10 | 11 | // BEGIN(function_shorthand) 12 | bool isNoble(int atomicNumber) => _nobleGases[atomicNumber] != null; 13 | // END(function_shorthand) 14 | 15 | void main() { 16 | print('Noble gases'); 17 | print('-----------'); 18 | for (int i = 0; i < 100; i++) { 19 | if (isNoble(i)) print('$i: ${_nobleGases[i]}'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /code/library-tour/get-data-from-server/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | HTML testing from Library Tour 7 | 8 | 9 | 10 |

HTML testing from Library Tour

11 | 12 |

Nothing to see here. Look in the console for output.

13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /code/library-tour/json-parse/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:convert' show JSON; 2 | 3 | void main() { 4 | // NOTE: Be sure to use double quotes ("), 5 | // not single quotes ('), inside the JSON string. 6 | // This string is JSON, not Dart. 7 | var jsonString = ''' 8 | [ 9 | {"score": 40}, 10 | {"score": 80} 11 | ] 12 | '''; 13 | 14 | var scores = JSON.decode(jsonString); 15 | assert(scores is List); 16 | 17 | var firstScore = scores[0]; 18 | assert(firstScore is Map); 19 | assert(firstScore['score'] == 40); 20 | } 21 | -------------------------------------------------------------------------------- /code/language-tour/number-conversion/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(number_conversion) 3 | // String -> int 4 | var one = int.parse('1'); 5 | assert(one == 1); 6 | 7 | // String -> double 8 | var onePointOne = double.parse('1.1'); 9 | assert(onePointOne == 1.1); 10 | 11 | // int -> String 12 | String oneAsString = 1.toString(); 13 | assert(oneAsString == '1'); 14 | 15 | // double -> String 16 | String piAsString = 3.14159.toStringAsFixed(2); 17 | assert(piAsString == '3.14'); 18 | // END(number_conversion) 19 | } 20 | -------------------------------------------------------------------------------- /code/language-tour/op-increment-decrement/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var a, b; 3 | 4 | a = 0; 5 | b = ++a; // Increment a before b gets its value. 6 | assert(a == b); // 1 == 1 7 | 8 | a = 0; 9 | b = a++; // Increment a AFTER b gets its value. 10 | assert(a != b); // 1 != 0 11 | 12 | a = 0; 13 | b = --a; // Decrement a before b gets its value. 14 | assert(a == b); // -1 == -1 15 | 16 | a = 0; 17 | b = a--; // Decrement a AFTER b gets its value. 18 | assert(a != b); // -1 != 0 19 | 20 | print('a = $a; b = $b'); 21 | } 22 | -------------------------------------------------------------------------------- /code/library-tour/client-server/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | import 'dart:convert'; 3 | import 'dart:async'; 4 | 5 | Future main() async { 6 | var url = Uri.parse('http://127.0.0.1:8888/languages/dart'); 7 | var httpClient = new HttpClient(); 8 | var request = await httpClient.getUrl(url); 9 | print('have request'); 10 | var response = await request.close(); 11 | print('have response'); 12 | var data = await response.transform(UTF8.decoder).toList(); 13 | var body = data.join(''); 14 | print(body); 15 | httpClient.close(); 16 | } 17 | -------------------------------------------------------------------------------- /code/language-tour/flow-if-else/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Person { 2 | void bringRainCoat() {} 3 | void wearJacket() {} 4 | } 5 | 6 | class Vehicle { 7 | void putTopDown() {} 8 | } 9 | 10 | bool isRaining() { 11 | return false; 12 | } 13 | 14 | bool isSnowing() { 15 | return false; 16 | } 17 | 18 | void main() { 19 | var you = new Person(); 20 | var car = new Vehicle(); 21 | 22 | if (isRaining()) { 23 | you.bringRainCoat(); 24 | } else if (isSnowing()) { 25 | you.wearJacket(); 26 | } else { 27 | car.putTopDown(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /code/language-tour/function-example/bin/main.dart: -------------------------------------------------------------------------------- 1 | var _nobleGases = { 2 | // Keys Values 3 | 2: 'helium', 4 | 10: 'neon', 5 | 18: 'argon', 6 | 36: 'krypton', 7 | 54: 'xenon', 8 | 86: 'radon', 9 | }; 10 | 11 | // BEGIN(function_example) 12 | bool isNoble(int atomicNumber) { 13 | return _nobleGases[atomicNumber] != null; 14 | } 15 | // END(function_example) 16 | 17 | void main() { 18 | print('Noble gases'); 19 | print('-----------'); 20 | for (int i = 0; i < 100; i++) { 21 | if (isNoble(i)) print('$i: ${_nobleGases[i]}'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /code/library-tour/async-await/async-await-web/web/styles.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | background-color: #F8F8F8; 4 | font-family: 'Open Sans', sans-serif; 5 | font-size: 14px; 6 | font-weight: normal; 7 | line-height: 1.2em; 8 | margin: 15px; 9 | } 10 | 11 | h1, p { 12 | color: #333; 13 | } 14 | 15 | #sample_container_id { 16 | width: 100%; 17 | height: 400px; 18 | position: relative; 19 | border: 1px solid #ccc; 20 | background-color: #fff; 21 | } 22 | 23 | #sample_text_id { 24 | font-size: 24pt; 25 | text-align: center; 26 | margin-top: 140px; 27 | } 28 | -------------------------------------------------------------------------------- /code/language-tour/cascade-operator/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | web_app 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /code/language-tour/function-omitting-types/bin/main.dart: -------------------------------------------------------------------------------- 1 | var _nobleGases = { 2 | // Keys Values 3 | 2: 'helium', 4 | 10: 'neon', 5 | 18: 'argon', 6 | 36: 'krypton', 7 | 54: 'xenon', 8 | 86: 'radon', 9 | }; 10 | 11 | // BEGIN(function_omitting_types) 12 | bool isNoble(int atomicNumber) { 13 | return _nobleGases[atomicNumber] != null; 14 | } 15 | // END(function_omitting_types) 16 | 17 | void main() { 18 | print('Noble gases'); 19 | print('-----------'); 20 | for (int i = 0; i < 100; i++) { 21 | if (isNoble(i)) print('$i: ${_nobleGases[i]}'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /code/language-tour/adjacent-string-literals/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(adjacent_string_literals) 3 | var s1 = 'String ' 4 | 'concatenation' 5 | " works even over line breaks."; 6 | assert(s1 == 7 | 'String concatenation works even over ' 8 | 'line breaks.'); 9 | 10 | var s2 = 'The + operator ' + 'works, as well.'; 11 | assert(s2 == 'The + operator works, as well.'); 12 | // END(adjacent_string_literals) 13 | 14 | print(s1); // String concatenation works even over line breaks. 15 | print(s2); // The + operator works, as well. 16 | } 17 | -------------------------------------------------------------------------------- /code/language-tour/reference/abstract.dart: -------------------------------------------------------------------------------- 1 | // This class is declared abstract and thus 2 | // can't be instantiated. 3 | abstract class AbstractContainer { 4 | // ...Define constructors, fields, methods... 5 | 6 | void updateChildren(); // Abstract method. 7 | } 8 | 9 | class SpecializedContainer extends AbstractContainer { 10 | // ...Define more constructors, fields, methods... 11 | 12 | void updateChildren() { 13 | // ...Implement updateChildren()... 14 | } 15 | // Abstract method causes a warning but doesn't prevent instantiatation. 16 | /*void doSomething(); */ 17 | } 18 | -------------------------------------------------------------------------------- /code/library-tour/duration/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var y2k = new DateTime.utc(2000); 3 | 4 | // Add one year. 5 | var y2001 = y2k.add(const Duration(days: 366)); 6 | assert(y2001.year == 2001); 7 | 8 | // Subtract 30 days. 9 | var december2000 = y2001.subtract(const Duration(days: 30)); 10 | assert(december2000.year == 2000); 11 | assert(december2000.month == 12); 12 | 13 | // Calculate the difference between two dates. 14 | // Returns a Duration object. 15 | var duration = y2001.difference(y2k); 16 | assert(duration.inDays == 366); // y2k was a leap year. 17 | } 18 | -------------------------------------------------------------------------------- /code/library-tour/reference/rootIsolate.dart: -------------------------------------------------------------------------------- 1 | //import 'dart:isolate'; 2 | // 3 | //childIsolate() { 4 | // port.receive((msg, replyTo) { 5 | // print('doing some work'); 6 | // if (replyTo != null) replyTo.send('shutdown'); 7 | // }); 8 | //} 9 | // 10 | //main() { 11 | // var sender = spawnFunction(childIsolate); 12 | // var receiver = new ReceivePort(); 13 | // receiver.receive((msg, _) { 14 | // if (msg == 'shutdown') { 15 | // print('shutting down'); 16 | // receiver.close(); 17 | // } 18 | // }); 19 | // sender.send('do work please', receiver.toSendPort()); 20 | //} 21 | -------------------------------------------------------------------------------- /code/language-tour/continue_example/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Candidate { 2 | Candidate(this.name, this.yearsExperience); 3 | 4 | String name; 5 | num yearsExperience; 6 | void interview() => print("interviewed $name"); 7 | } 8 | 9 | List candidates = new List(); 10 | 11 | void main() { 12 | candidates.add(new Candidate("Pam", 10)); 13 | candidates.add(new Candidate("Bob", 4)); 14 | 15 | for (int i = 0; i < candidates.length; i++) { 16 | var candidate = candidates[i]; 17 | if (candidate.yearsExperience < 5) { 18 | continue; 19 | } 20 | candidate.interview(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /code/language-tour/rectangle/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Rectangle { 2 | num left; 3 | num top; 4 | num width; 5 | num height; 6 | 7 | Rectangle(this.left, this.top, this.width, this.height); 8 | 9 | // Define two calculated properties: right and bottom. 10 | num get right => left + width; 11 | set right(num value) => left = value - width; 12 | num get bottom => top + height; 13 | set bottom(num value) => top = value - height; 14 | } 15 | 16 | void main() { 17 | var rect = new Rectangle(3, 4, 20, 15); 18 | assert(rect.left == 3); 19 | rect.right = 12; 20 | assert(rect.left == -8); 21 | } 22 | -------------------------------------------------------------------------------- /code/language-tour/string-literals/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(string_const_literals) 3 | // These work in a const string. 4 | const aConstNum = 0; 5 | const aConstBool = true; 6 | const aConstString = 'a constant string'; 7 | 8 | // These do NOT work in a const string. 9 | var aNum = 0; 10 | var aBool = true; 11 | var aString = 'a string'; 12 | const aConstList = const [1, 2, 3]; 13 | 14 | const validConstString = '$aConstNum $aConstBool $aConstString'; 15 | // const invalidConstString = '$aNum $aBool $aString $aConstList'; 16 | print(validConstString); 17 | // END(string_interpolation) 18 | } 19 | -------------------------------------------------------------------------------- /code/language-tour/list-map-default-function-parameters/bin/main.dart: -------------------------------------------------------------------------------- 1 | void doStuff( 2 | {List list = const [1, 2, 3], 3 | Map gifts = const { 4 | 'first': 'paper', 5 | 'second': 'cotton', 6 | 'third': 'leather' 7 | }}) { 8 | print('list: $list'); 9 | print('gifts: $gifts'); 10 | } 11 | 12 | void main() { 13 | // Use the default values for both parameters. 14 | doStuff(); 15 | 16 | // Use the default values for the "gifts" parameter. 17 | doStuff(list: [4, 5, 6]); 18 | 19 | // Don't use the default values for either parameter. 20 | doStuff(list: null, gifts: null); 21 | } 22 | -------------------------------------------------------------------------------- /code/language-tour/op-as/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Person { 2 | String firstName; 3 | 4 | Person.fromJson(Map data) { 5 | print('in Person'); 6 | } 7 | } 8 | 9 | class Employee extends Person { 10 | // Person does not have a default constructor; 11 | // you must call super.fromJson(data). 12 | Employee.fromJson(Map data) : super.fromJson(data) { 13 | print('in Employee'); 14 | } 15 | } 16 | 17 | void main() { 18 | var emp = new Employee.fromJson({}); 19 | 20 | // Prints: 21 | // in Person 22 | // in Employee 23 | if (emp is Person) { 24 | // Type check 25 | emp.firstName = 'Bob'; 26 | } 27 | emp.firstName = 'Bob'; 28 | } 29 | -------------------------------------------------------------------------------- /code/language-tour/reference/doc_comments.dart: -------------------------------------------------------------------------------- 1 | //BEGIN 2 | /// A domesticated South American camelid (Lama glama). 3 | /// 4 | /// Andean cultures have used llamas as meat and pack 5 | /// animals since pre-Hispanic times. 6 | class Llama { 7 | String name; 8 | 9 | /// Feeds your llama [Food]. 10 | /// 11 | /// The typical llama eats one bale of hay per week. 12 | void feed(Food food) { 13 | // ... 14 | } 15 | 16 | /// Exercises your llama with an [activity] for 17 | /// [timeLimit] minutes. 18 | void exercise(Activity activity, int timeLimit) { 19 | // ... 20 | } 21 | } 22 | //END 23 | 24 | class Food {} 25 | 26 | class Activity {} 27 | -------------------------------------------------------------------------------- /code/language-tour/const-vs-const/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // Note: [] creates an empty list. 3 | // const [] creates an empty, immutable list (EIA). 4 | var foo = const []; // foo is currently an EIA. 5 | final bar = const []; // bar will always be an EIA. 6 | const baz = const []; // baz is a compile-time constant EIA. 7 | 8 | print('$foo, $bar, $baz'); // [], [], [] 9 | 10 | // You can change the value of a non-final, non-const variable, 11 | // even if it used to have a const value. 12 | foo = []; 13 | 14 | // You can't change the value of a final or const variable. 15 | // bar = []; // Unhandled exception. 16 | // baz = []; // Unhandled exception. 17 | } 18 | -------------------------------------------------------------------------------- /code/language-tour/vector/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Vector { 2 | final int x; 3 | final int y; 4 | const Vector(this.x, this.y); 5 | 6 | /// Overrides + (a + b). 7 | Vector operator +(Vector v) { 8 | return new Vector(x + v.x, y + v.y); 9 | } 10 | 11 | /// Overrides - (a - b). 12 | Vector operator -(Vector v) { 13 | return new Vector(x - v.x, y - v.y); 14 | } 15 | } 16 | 17 | void main() { 18 | final v = new Vector(2, 3); 19 | final w = new Vector(2, 2); 20 | 21 | // v == (2, 3) 22 | assert(v.x == 2 && v.y == 3); 23 | 24 | // v + w == (4, 5) 25 | assert((v + w).x == 4 && (v + w).y == 5); 26 | 27 | // v - w == (0, 1) 28 | assert((v - w).x == 0 && (v - w).y == 1); 29 | } 30 | -------------------------------------------------------------------------------- /code/language-tour/final-initialization/bin/main.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(final_initialization) 2 | final String name = 'Bob'; // Or: final String name = 'Bob'; 3 | // name = 'Alice'; // Uncommenting this causes an error 4 | // END(final_initialization) 5 | 6 | final String name2 = 'Roberta'; 7 | 8 | class Robert { 9 | final String nickname; 10 | 11 | Robert(this.nickname); 12 | 13 | @override 14 | String toString() => 'This Robert\'s nickname is $nickname.'; 15 | } 16 | 17 | void main() { 18 | var r1 = new Robert(null); 19 | var r2 = new Robert(name); 20 | var r3 = new Robert('Robby'); 21 | var r4 = new Robert(name2); 22 | 23 | print(r1); 24 | print(r2); 25 | print(r3); 26 | print(r4); 27 | } 28 | -------------------------------------------------------------------------------- /code/library-tour/client-server/bin/http_server.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | import 'dart:async'; 3 | 4 | Future main() async { 5 | void dartHandler(HttpRequest request) { 6 | request.response.headers.contentType = new ContentType('text', 'plain'); 7 | request.response.write('Sending a response'); 8 | request.response.close(); 9 | } 10 | 11 | var requests = await HttpServer.bind('127.0.0.1', 8888); 12 | await for (var request in requests) { 13 | print('Got request for ${request.uri.path}'); 14 | if (request.uri.path == '/languages/dart') { 15 | dartHandler(request); 16 | } else { 17 | request.response.write('Not found'); 18 | request.response.close(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /code/library-tour/future-then/web/styles.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | background-color: #F8F8F8; 4 | font-family: 'Open Sans', sans-serif; 5 | font-size: 14px; 6 | font-weight: normal; 7 | line-height: 1.2em; 8 | margin: 15px; 9 | } 10 | 11 | h1, p { 12 | color: #333; 13 | } 14 | 15 | #sample_container_id { 16 | width: 100%; 17 | height: 400px; 18 | position: relative; 19 | border: 1px solid #ccc; 20 | background-color: #fff; 21 | } 22 | 23 | #sample_text_id { 24 | font-size: 24pt; 25 | text-align: center; 26 | margin-top: 140px; 27 | } 28 | 29 | #fun-with-nodes div { 30 | border: 1px solid black; 31 | margin: 1em; 32 | } 33 | 34 | .warning { 35 | background-color: pink; 36 | font-style: italic; 37 | } -------------------------------------------------------------------------------- /code/language-tour/flow/break-continue/web/main.dart: -------------------------------------------------------------------------------- 1 | bool shutDownRequested() => true; 2 | void processIncomingRequests() {} 3 | 4 | class Candidate { 5 | int yearsExperience = 2; 6 | void interview() {} 7 | } 8 | 9 | void main() { 10 | while (true) { 11 | if (shutDownRequested()) break; 12 | processIncomingRequests(); 13 | } 14 | 15 | var candidates = [new Candidate(), new Candidate(), new Candidate()]; 16 | 17 | for (int i = 0; i < candidates.length; i++) { 18 | var candidate = candidates[i]; 19 | if (candidate.yearsExperience < 5) { 20 | continue; 21 | } 22 | candidate.interview(); 23 | } 24 | 25 | candidates.where((c) => c.yearsExperience >= 5).forEach((c) => c.interview()); 26 | } 27 | -------------------------------------------------------------------------------- /code/library-tour/iterator/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:collection'; 2 | 3 | class Process { 4 | // Represents a process... 5 | } 6 | 7 | class ProcessIterator implements Iterator { 8 | @override 9 | Process current; 10 | @override 11 | bool moveNext() { 12 | return false; 13 | } 14 | } 15 | 16 | // A mythical class that lets you iterate through all 17 | // processes. Extends a subclass of Iterable. 18 | class Processes extends IterableBase { 19 | @override 20 | final Iterator iterator = new ProcessIterator(); 21 | } 22 | 23 | void main() { 24 | // Iterable objects can be used with for-in. 25 | for (var process in new Processes()) { 26 | // Do something with the process. 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /code/language-tour/flow/assert_example/web/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var text = 'Hi'; 3 | var number = 99; 4 | String urlString = 'https://github.com/'; 5 | bool showMessage = true; 6 | 7 | // Make sure the variable has a non-null value. 8 | assert(text != null); 9 | 10 | // Make sure the value is less than 100. 11 | assert(number < 100); 12 | 13 | // Make sure this is an https URL. 14 | if (!showMessage) { 15 | assert(urlString.startsWith('https')); 16 | } else { 17 | assert(urlString.startsWith('https'), 18 | 'URL ($urlString) should start with "https".'); 19 | } 20 | 21 | print('About to call assert(false).'); 22 | assert(false); 23 | print('Oops, you weren\'t running in checked mode.'); 24 | } 25 | -------------------------------------------------------------------------------- /code/language-tour/while-collection/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Candidate { 2 | Candidate(this.name, this.yearsExperience); 3 | 4 | String name; 5 | num yearsExperience; 6 | void interview() => print("interviewed $name"); 7 | } 8 | 9 | List candidates = new List(); 10 | 11 | void main() { 12 | candidates.add(new Candidate("Pam", 10)); 13 | candidates.add(new Candidate("Bob", 4)); 14 | candidates.add(new Candidate("Alice", 40)); 15 | 16 | // candidates.where((c) => c.yearsExperience >= 5) 17 | // .forEach((c) => c.interview()); 18 | 19 | candidates.where((c) => c.yearsExperience >= 5).forEach((c) => c.interview()); 20 | 21 | // for (var cand in candidates.where((c) => c.yearsExperience >= 5)) { 22 | // cand.interview(); 23 | // } 24 | } 25 | -------------------------------------------------------------------------------- /code/language-tour/cascade-operator/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Person { 2 | String firstName; 3 | String lastName; 4 | String nickname; 5 | 6 | @override 7 | String toString() => '$firstName $lastName'; 8 | String toTmiString() => '$firstName "$nickname" $lastName'; 9 | String toBondianString() => '$lastName, ${toString()}'; 10 | String toSwappedString() => '$lastName, $firstName'; 11 | String capitalize() => toString().toUpperCase(); 12 | } 13 | 14 | void main() { 15 | var p1 = new Person() 16 | ..firstName = 'Kathy' 17 | ..lastName = 'Walrath' 18 | ..nickname = 'The Mad Knitter'; 19 | print(p1); 20 | print(p1.toSwappedString()); 21 | print(p1.toTmiString()); 22 | print(p1.toBondianString()); 23 | print(p1.capitalize()); 24 | } 25 | -------------------------------------------------------------------------------- /code/language-tour/async-await/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | 3 | const String expectedVersion = '1.0.0'; 4 | const bool dev = true; 5 | 6 | String lookUpVersionSync() => '1.0.0'; 7 | Future lookUpVersionAsync() async => '1.0.0'; 8 | 9 | Future lookUpVersion() async { 10 | if (dev) return 'dev-' + await loadFromServer(); 11 | return '1.0.0'; 12 | } 13 | 14 | Future loadFromServer() async => '1.0.0'; 15 | 16 | Future checkVersion() async { 17 | var version = await lookUpVersion(); 18 | if (version == expectedVersion) { 19 | print(expectedVersion); 20 | } else { 21 | print('Unexpected version: $version'); 22 | } 23 | } 24 | 25 | Future main() async { 26 | checkVersion(); 27 | print('In main: version is ${await lookUpVersion()}'); 28 | } 29 | -------------------------------------------------------------------------------- /code/library-tour/decode-utf-8/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:convert' show UTF8; 2 | 3 | void main() { 4 | var string = UTF8.decode([ 5 | 0xc3, 6 | 0x8e, 7 | 0xc3, 8 | 0xb1, 9 | 0xc5, 10 | 0xa3, 11 | 0xc3, 12 | 0xa9, 13 | 0x72, 14 | 0xc3, 15 | 0xb1, 16 | 0xc3, 17 | 0xa5, 18 | 0xc5, 19 | 0xa3, 20 | 0xc3, 21 | 0xae, 22 | 0xc3, 23 | 0xb6, 24 | 0xc3, 25 | 0xb1, 26 | 0xc3, 27 | 0xa5, 28 | 0xc4, 29 | 0xbc, 30 | 0xc3, 31 | 0xae, 32 | 0xc5, 33 | 0xbe, 34 | 0xc3, 35 | 0xa5, 36 | 0xc5, 37 | 0xa3, 38 | 0xc3, 39 | 0xae, 40 | 0xe1, 41 | 0xbb, 42 | 0x9d, 43 | 0xc3, 44 | 0xb1 45 | ]); 46 | print(string); // 'Îñţérñåţîöñåļîžåţîờñ' 47 | } 48 | -------------------------------------------------------------------------------- /code/library-tour/async-await/async-await-web/web/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | import 'dart:html'; 3 | 4 | String url = 'humans.txt'; 5 | 6 | void main() { 7 | printRobots(); 8 | printRobotsAndHandleErrors(); 9 | querySelector("#button1").onClick.listen(handleClick); 10 | } 11 | 12 | void handleClick(MouseEvent event) { 13 | print('click!'); 14 | } 15 | 16 | Future printRobots() async { 17 | var result = await HttpRequest.getString(url); 18 | print(result); 19 | // Should handle errors here. 20 | } 21 | 22 | Future printRobotsAndHandleErrors() async { 23 | try { 24 | var result = await HttpRequest.getString(url); 25 | print(result); 26 | } catch (e) { 27 | // Handle or ignore the error. 28 | print('Oops, couldn\'t get $url: $e'); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /code/language-tour/enum-switch/bin/main.dart: -------------------------------------------------------------------------------- 1 | enum Color { red, green, blue } 2 | 3 | void main() { 4 | assert(Color.red.index == 0); 5 | assert(Color.green.index == 1); 6 | assert(Color.blue.index == 2); 7 | assert(Color.blue.toString() == 'Color.blue'); 8 | 9 | List colors = Color.values; 10 | assert(colors[2] == Color.blue); 11 | 12 | // NOTE: aColor must be explicitly typed for the analyzer 13 | // to check for complete coverage in the switch statement. 14 | Color aColor = Color.blue; 15 | 16 | switch (aColor) { 17 | case Color.red: 18 | print('Red as roses!'); 19 | break; 20 | case Color.green: 21 | print('Green as grass!'); 22 | break; 23 | default: // Without this, you see a WARNING. 24 | print(aColor); // 'Color.blue' 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /code/language-tour/function-equality-2/bin/main.dart: -------------------------------------------------------------------------------- 1 | void foo() {} // A top-level function 2 | 3 | class A { 4 | static void bar() {} // A static method 5 | void baz() {} // An instance method 6 | } 7 | 8 | void main() { 9 | var x; 10 | 11 | // Comparing top-level functions. 12 | x = foo; 13 | assert(foo == x); 14 | 15 | // Comparing static methods. 16 | x = A.bar; 17 | assert(A.bar == x); 18 | 19 | // Comparing instance methods. 20 | var v = new A(); // Instance #1 of A 21 | var w = new A(); // Instance #2 of A 22 | var y = w; 23 | x = w.baz; 24 | 25 | // These closures refer to the same instance (#2), 26 | // so they're equal. 27 | assert(y.baz == x); 28 | 29 | // These closures refer to different instances, 30 | // so they're unequal. 31 | assert(v.baz != w.baz); 32 | } 33 | -------------------------------------------------------------------------------- /code/library-tour/number-tests/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | assert(int.parse('42') == 42); 3 | assert(int.parse('0x42') == 66); 4 | assert(double.parse('0.50') == 0.5); 5 | 6 | assert(num.parse('42') is int); 7 | assert(num.parse('0x42') is int); 8 | assert(num.parse('0.50') is double); 9 | 10 | // Specify the base. 11 | assert(int.parse('42', radix: 16) == 66); 12 | 13 | // Convert an int to a string. 14 | assert(42.toString() == '42'); 15 | 16 | // Convert a double to a string. 17 | assert(123.456.toString() == '123.456'); 18 | 19 | // Specify the number of digits after the decimal. 20 | assert(123.456.toStringAsFixed(2) == '123.46'); 21 | 22 | // Specify the number of significant figures. 23 | assert(123.456.toStringAsPrecision(2) == '1.2e+2'); 24 | assert(double.parse('1.2e+2') == 120.0); 25 | } 26 | -------------------------------------------------------------------------------- /code/language-tour/reference/map_literal.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(map_literal) 2 | var gifts = { 3 | // Keys Values 4 | 'first': 'partridge', 5 | 'second': 'turtledoves', 6 | 'fifth': 'golden rings' 7 | }; 8 | 9 | var nobleGases = { 10 | // Keys Values 11 | 2: 'helium', 12 | 10: 'neon', 13 | 18: 'argon', 14 | }; 15 | // END(map_literal) 16 | 17 | main() { 18 | // BEGIN(map_const_literal) 19 | final constantMap = const {2: 'helium', 10: 'neon', 18: 'argon',}; 20 | 21 | // constantMap[2] = 'Helium'; // Uncommenting this causes an error. 22 | // END(map_const_literal) 23 | 24 | nobleGases[2] = 'Helium'; // You can do this. 25 | try { 26 | constantMap[2] = 'Helium'; // You can't do this. 27 | } catch (e) { 28 | print('Tsk tsk! $e'); 29 | } 30 | } 31 | 32 | //36: 'krypton', 33 | //54: 'xenon', 34 | //86: 'radon', 35 | -------------------------------------------------------------------------------- /code/language-tour/imposter/bin/main.dart: -------------------------------------------------------------------------------- 1 | // A person. The implicit interface contains greet(). 2 | class Person { 3 | // In the interface, but visible only in this library. 4 | final _name; 5 | 6 | // Not in the interface, since this is a constructor. 7 | Person(this._name); 8 | 9 | // In the interface. 10 | String greet(String who) => 'Hello, $who. I am $_name.'; 11 | } 12 | 13 | // An implementation of the Person interface. 14 | class Imposter implements Person { 15 | // We have to define this, but we don't use it. 16 | @override 17 | final _name = ""; 18 | 19 | @override 20 | String greet(String who) => 'Hi $who. Do you know who I am?'; 21 | } 22 | 23 | String greetBob(Person person) => person.greet('bob'); 24 | 25 | void main() { 26 | print(greetBob(new Person('kathy'))); 27 | print(greetBob(new Imposter())); 28 | } 29 | -------------------------------------------------------------------------------- /code/library-tour/write-file/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | void overwriteFile() { 4 | var logFile = new File('log.txt'); 5 | var sink = logFile.openWrite(); 6 | sink.write('FILE ACCESSED ${new DateTime.now()}\n'); 7 | sink.close(); 8 | } 9 | 10 | void appendToFile() { 11 | var logFile = new File('betterLog.txt'); 12 | var sink = logFile.openWrite(mode: FileMode.APPEND); 13 | sink.write('FILE ACCESSED ${new DateTime.now()}\n'); 14 | sink.close(); 15 | } 16 | 17 | void writeBinary() { 18 | var binaryFile = new File('copyOfIcon.ico'); 19 | var sink = binaryFile.openWrite(); 20 | var icoFile = new File('icon.ico'); // UNUSED in CH03 21 | var data = icoFile.openSync().readSync(icoFile.lengthSync()); //UNUSED in CH03 22 | sink.add(data); 23 | sink.close(); 24 | } 25 | 26 | void main() { 27 | overwriteFile(); 28 | appendToFile(); 29 | writeBinary(); 30 | } 31 | -------------------------------------------------------------------------------- /code/library-tour/collection-any-every/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // BEGIN(collection-any-every) 3 | var teas = ['green', 'black', 'chamomile', 'earl grey']; 4 | 5 | // Chamomile is not caffeinated. 6 | bool isDecaffeinated(String teaName) => teaName == 'chamomile'; 7 | 8 | // Use where() to find only the items that return true 9 | // from the provided function. 10 | var decaffeinatedTeas = teas.where((tea) => isDecaffeinated(tea)); 11 | // or teas.where(isDecaffeinated) 12 | 13 | // Use any() to check whether at least one item in the 14 | // collection satisfies a condition. 15 | assert(teas.any(isDecaffeinated)); 16 | 17 | // Use every() to check whether all the items in a 18 | // collection satisfy a condition. 19 | assert(!teas.every(isDecaffeinated)); 20 | // END(collection-any-every) 21 | 22 | teas.forEach(print); 23 | decaffeinatedTeas.forEach(print); 24 | } 25 | -------------------------------------------------------------------------------- /archive/book.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Dart: Up and Running 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /code/library-tour/map-keys/bin/main.dart: -------------------------------------------------------------------------------- 1 | class Person { 2 | final String firstName, lastName; 3 | 4 | Person(this.firstName, this.lastName); 5 | 6 | // Override hashCode using strategy from Effective Java, 7 | // Chapter 11. 8 | @override 9 | int get hashCode { 10 | int result = 17; 11 | result = 37 * result + firstName.hashCode; 12 | result = 37 * result + lastName.hashCode; 13 | return result; 14 | } 15 | 16 | // You should generally implement operator == if you 17 | // override hashCode. 18 | @override 19 | bool operator ==(dynamic other) { 20 | if (other is! Person) return false; 21 | Person person = other; 22 | return (person.firstName == firstName && person.lastName == lastName); 23 | } 24 | } 25 | 26 | void main() { 27 | var p1 = new Person('bob', 'smith'); 28 | var p2 = new Person('bob', 'smith'); 29 | var p3 = 'not a person'; 30 | assert(p1.hashCode == p2.hashCode); 31 | assert(p1 == p2); 32 | assert(p1 != p3); 33 | } 34 | -------------------------------------------------------------------------------- /code/library-tour/encode-utf-8/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:convert' show UTF8; 2 | 3 | void main() { 4 | List expected = [ 5 | 0xc3, 6 | 0x8e, 7 | 0xc3, 8 | 0xb1, 9 | 0xc5, 10 | 0xa3, 11 | 0xc3, 12 | 0xa9, 13 | 0x72, 14 | 0xc3, 15 | 0xb1, 16 | 0xc3, 17 | 0xa5, 18 | 0xc5, 19 | 0xa3, 20 | 0xc3, 21 | 0xae, 22 | 0xc3, 23 | 0xb6, 24 | 0xc3, 25 | 0xb1, 26 | 0xc3, 27 | 0xa5, 28 | 0xc4, 29 | 0xbc, 30 | 0xc3, 31 | 0xae, 32 | 0xc5, 33 | 0xbe, 34 | 0xc3, 35 | 0xa5, 36 | 0xc5, 37 | 0xa3, 38 | 0xc3, 39 | 0xae, 40 | 0xe1, 41 | 0xbb, 42 | 0x9d, 43 | 0xc3, 44 | 0xb1 45 | ]; 46 | 47 | List encoded = UTF8.encode('Îñţérñåţîöñåļîžåţîờñ'); 48 | 49 | assert(() { 50 | if (encoded.length != expected.length) return false; 51 | for (int i = 0; i < encoded.length; i++) { 52 | if (encoded[i] != expected[i]) return false; 53 | } 54 | return true; 55 | }); 56 | } 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Dart: Up and Running - ARCHIVE 2 | ============= 3 | 4 | This is the book _Dart: Up and Running_ by Kathy Walrath and Seth Ladd. 5 | More info: http://shop.oreilly.com/product/0636920025719.do 6 | 7 | **The text and code in this repo are old.** 8 | 9 | The latest text is in the 10 | [dart-lang/site-www](https://github.com/dart-lang/site-www/) repo under 11 | [/src/_guides/language/language-tour](https://github.com/dart-lang/site-www/blob/master/src/_guides/language/language-tour.md) and 12 | [/src/_guides/libraries/library-tour](https://github.com/dart-lang/site-www/blob/master/src/_guides/libraries/library-tour.md). 13 | 14 | To see the **generated version** of these docs, see the 15 | [Language Tour](https://www.dartlang.org/guides/language/language-tour) 16 | and [Library Tour](https://www.dartlang.org/guides/libraries/library-tour) 17 | on [dartlang.org](https://www.dartlang.org/). 18 | 19 | The latest code is under [dart-lang/site-www/tree/master/examples](https://github.com/dart-lang/site-www/tree/master/examples). 20 | -------------------------------------------------------------------------------- /code/library-tour/post-data-to-server/web/main.dart: -------------------------------------------------------------------------------- 1 | // XXX: Now that it's converted to async-await, we really should run this. 2 | 3 | import 'dart:async'; 4 | import 'dart:html'; 5 | 6 | String encodeMap(Map data) { 7 | return data.keys.map((k) { 8 | return '${Uri.encodeComponent(k)}=' + '${Uri.encodeComponent(data[k])}'; 9 | }).join('&'); 10 | } 11 | 12 | void loadEnd(HttpRequest request) { 13 | if (request.status != 200) { 14 | print('Uh oh, error: ${request.status}'); 15 | return; 16 | } else { 17 | print('Data has been posted'); 18 | } 19 | } 20 | 21 | Future main() async { 22 | var dataUrl = '/registrations/create'; 23 | var data = {'dart': 'fun', 'editor': 'productive'}; 24 | var encodedData = encodeMap(data); 25 | 26 | var httpRequest = new HttpRequest(); 27 | httpRequest.open('POST', dataUrl); 28 | httpRequest.setRequestHeader( 29 | 'Content-type', 'application/x-www-form-urlencoded'); 30 | httpRequest.send(encodedData); 31 | await httpRequest.onLoadEnd.first; 32 | loadEnd(httpRequest); 33 | } 34 | -------------------------------------------------------------------------------- /code/language-tour/generics-base-class/bin/main.dart: -------------------------------------------------------------------------------- 1 | class SomeBaseClass {} 2 | 3 | // T must be SomeBaseClass or one of its descendants. 4 | class Foo { 5 | // Implementation goes here. 6 | @override 7 | String toString() => 'Foo<$T>'; 8 | } 9 | 10 | class Extender extends SomeBaseClass { 11 | // Implementation goes here. 12 | } 13 | 14 | void main() { 15 | // It's OK to use SomeBaseClass or any of its subclasses inside <>. 16 | var someBaseClassFoo = new Foo(); 17 | var extenderFoo = new Foo(); 18 | 19 | // It's also OK to use no <> at all. 20 | var foo = new Foo(); 21 | 22 | // Specifying any non-SomeBaseClass type results in a warning and, in 23 | // checked mode, a runtime error. 24 | // var objectFoo = new Foo(); 25 | 26 | // Normal mode: Foo, Foo, Foo, Foo 27 | // print('$someBaseClassFoo, $extenderFoo, $foo, $objectFoo'); 28 | 29 | // Foo, Foo, Foo 30 | print('$someBaseClassFoo, $extenderFoo, $foo'); 31 | } 32 | -------------------------------------------------------------------------------- /code/library-tour/read-file/bin/read_file.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | import 'dart:convert'; 3 | import 'dart:async'; 4 | 5 | void readFileStreamApi() { 6 | var config = new File('config.txt'); 7 | Stream> inputStream = config.openRead(); 8 | 9 | inputStream.transform(UTF8.decoder).transform(new LineSplitter()).listen( 10 | (String line) { 11 | print('Got ${line.length} characters from stream'); 12 | }, onDone: () { 13 | print('file is now closed'); 14 | }, onError: (e) { 15 | print(e); 16 | }); 17 | } 18 | 19 | Future readFileAwaitFor() async { 20 | var config = new File('config.txt'); 21 | Stream> inputStream = config.openRead(); 22 | 23 | var lines = inputStream.transform(UTF8.decoder).transform(new LineSplitter()); 24 | try { 25 | await for (var line in lines) { 26 | print('Got ${line.length} characters from stream'); 27 | } 28 | print('file is now closed'); 29 | } catch (e) { 30 | print(e); 31 | } 32 | } 33 | 34 | Future main() async { 35 | await readFileAwaitFor(); 36 | print('----------'); 37 | readFileStreamApi(); 38 | } 39 | -------------------------------------------------------------------------------- /code/language-tour/flow/for-loops/web/main.dart: -------------------------------------------------------------------------------- 1 | void doFor() { 2 | //BEGIN 3 | var message = new StringBuffer("Dart is fun"); 4 | for (var i = 0; i < 5; i++) { 5 | message.write('!'); 6 | } 7 | //END 8 | print(message); 9 | } 10 | 11 | void doClosures() { 12 | print('do closures right: 0 1'); 13 | 14 | //BEGIN 15 | var callbacks = []; 16 | for (var i = 0; i < 2; i++) { 17 | callbacks.add(() => print(i)); 18 | } 19 | callbacks.forEach((c) => c()); 20 | //END 21 | } 22 | 23 | void doCollection() { 24 | print('do collection'); 25 | 26 | //BEGIN 27 | var collection = [0, 1, 2]; 28 | for (var x in collection) { 29 | print(x); // 0 1 2 30 | } 31 | //END 32 | } 33 | 34 | class Candidate { 35 | void interview() {} 36 | } 37 | 38 | void doForIn() { 39 | List candidates = [ 40 | new Candidate(), 41 | new Candidate(), 42 | new Candidate() 43 | ]; 44 | 45 | //BEGIN 46 | candidates.forEach((candidate) => candidate.interview()); 47 | //END 48 | } 49 | 50 | void main() { 51 | doFor(); 52 | doClosures(); 53 | doCollection(); 54 | doForIn(); 55 | } 56 | -------------------------------------------------------------------------------- /code/language-tour/factory-constructor/bin/main.dart: -------------------------------------------------------------------------------- 1 | // BEGIN(factory_constructor) 2 | class Logger { 3 | final String name; 4 | bool mute = false; 5 | 6 | // _cache is library-private, thanks to the _ in front 7 | // of its name. 8 | static final Map _cache = {}; 9 | 10 | factory Logger(String name) { 11 | if (_cache.containsKey(name)) { 12 | return _cache[name]; 13 | } else { 14 | final logger = new Logger._internal(name); 15 | _cache[name] = logger; 16 | return logger; 17 | } 18 | } 19 | 20 | Logger._internal(this.name); 21 | 22 | void log(String msg) { 23 | if (!mute) { 24 | print(msg); 25 | } 26 | } 27 | } 28 | // END(factory_constructor) 29 | 30 | void main() { 31 | //BEGIN 32 | var logger = new Logger('UI'); 33 | logger.log('Button clicked'); 34 | //END 35 | 36 | var l1 = new Logger('log1'); 37 | var l2 = new Logger('log1'); 38 | var l3 = new Logger('log2'); 39 | 40 | assert(identical(l1, l2)); 41 | assert(l1 != l3); 42 | 43 | l1.log('${l1.name}: This is l1.'); 44 | l2.log('${l2.name}: This is l1_2.'); 45 | l3.log('${l3.name}: This is l2.'); 46 | } 47 | -------------------------------------------------------------------------------- /code/library-tour/math-tests/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math' as math; 2 | 3 | void main() { 4 | trig(); 5 | minmax(); 6 | constants(); 7 | random(); 8 | } 9 | 10 | void trig() { 11 | // Cosine 12 | assert(math.cos(math.PI) == -1.0); 13 | 14 | // Sine 15 | var degrees = 30; 16 | var radians = degrees * (math.PI / 180); 17 | // radians is now 0.52359. 18 | var sinOf30degrees = math.sin(radians); 19 | // sin 30° = 0.5 20 | assert((sinOf30degrees - 0.5).abs() < 0.01); 21 | } 22 | 23 | void minmax() { 24 | assert(math.max(1, 1000) == 1000); 25 | assert(math.min(1, -1000) == -1000); 26 | } 27 | 28 | void constants() { 29 | // See the Math library for additional constants. 30 | print(math.E); // 2.718281828459045 31 | print(math.PI); // 3.141592653589793 32 | print(math.SQRT2); // 1.4142135623730951 33 | } 34 | 35 | void random() { 36 | var random = new math.Random(); 37 | 38 | var nextDouble = random.nextDouble(); 39 | assert(nextDouble >= 0.0 && nextDouble < 1.0); // [0, 1) 40 | 41 | var nextInt = random.nextInt(10); 42 | assert(nextInt >= 0 && nextInt < 10); // [0 .. 9] 43 | 44 | var nextBool = random.nextBool(); // true or false 45 | 46 | print('$nextDouble, $nextInt, $nextBool'); 47 | } 48 | -------------------------------------------------------------------------------- /code/language-tour/mixins/bin/main.dart: -------------------------------------------------------------------------------- 1 | abstract class Musical { 2 | bool canPlayPiano = false; 3 | bool canCompose = false; 4 | bool canConduct = false; 5 | 6 | void entertainMe() { 7 | if (canPlayPiano) { 8 | print('Playing piano'); 9 | } else if (canConduct) { 10 | print('Waving hands'); 11 | } else { 12 | print('Humming to self'); 13 | } 14 | } 15 | } 16 | 17 | abstract class Aggressive { 18 | bool passive = false; 19 | } 20 | 21 | abstract class Demented { 22 | bool dangerous = false; 23 | } 24 | 25 | class Person { 26 | String name; 27 | Person(); 28 | Person.withName(this.name); 29 | } 30 | 31 | abstract class Performer { 32 | Performer(String name); 33 | String name; 34 | } 35 | 36 | class Musician extends Performer with Musical { 37 | Musician(String name) : super(name); 38 | } 39 | 40 | class Maestro extends Person with Musical, Aggressive, Demented { 41 | Maestro(String maestroName) { 42 | name = maestroName; 43 | canConduct = true; 44 | } 45 | } 46 | 47 | void main() { 48 | var director = new Maestro('Allen'); 49 | director.entertainMe(); // Waving hands 50 | 51 | var musician = new Musician('Kathy'); 52 | musician.canPlayPiano = true; 53 | musician.entertainMe(); // Playing piano 54 | } 55 | -------------------------------------------------------------------------------- /code/language-tour/generics/bin/main.dart: -------------------------------------------------------------------------------- 1 | //Why Use Generics? 2 | abstract class ObjectCache { 3 | Object getByKey(String key); 4 | void setByKey(String key, Object value); 5 | } 6 | 7 | abstract class StringCache { 8 | String getByKey(String key); 9 | void setByKey(String key, String value); 10 | } 11 | 12 | abstract class Cache { 13 | T getByKey(String key); 14 | void setByKey(String key, T value); 15 | } 16 | 17 | class View {} 18 | 19 | void main() { 20 | whyUseGenerics(); 21 | usingCollectionLiterals(); 22 | usingConstructors(); 23 | genericCollections(); 24 | } 25 | 26 | void whyUseGenerics() { 27 | var names = new List(); 28 | names.addAll(['Seth', 'Kathy', 'Lars']); 29 | // ... 30 | /*names.add(42);*/ // Fails in checked mode (succeeds in production mode). 31 | } 32 | 33 | void usingCollectionLiterals() { 34 | var names = ['Seth', 'Kathy', 'Lars']; 35 | var pages = { 36 | 'index.html': 'Homepage', 37 | 'robots.txt': 'Hints for web robots', 38 | 'humans.txt': 'We are people, not machines' 39 | }; 40 | } 41 | 42 | void usingConstructors() { 43 | var names = new List(); 44 | names.addAll(['Seth', 'Kathy', 'Lars']); 45 | var nameSet = new Set.from(names); 46 | 47 | var views = new Map(); 48 | } 49 | 50 | void genericCollections() { 51 | var names = new List(); 52 | names.addAll(['Seth', 'Kathy', 'Lars']); 53 | print(names is List); // true 54 | } 55 | -------------------------------------------------------------------------------- /code/library-tour/date/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // Get the current date and time. 3 | var now = new DateTime.now(); 4 | print(now); 5 | 6 | // Create a new DateTime with the local time zone. 7 | var y2k = new DateTime(2000); // January 1, 2000 8 | print(y2k); 9 | 10 | // Specify the month and day. 11 | y2k = new DateTime(2000, 1, 2); // January 2, 2000 12 | print(y2k); 13 | 14 | // Specify the date as a UTC time. 15 | y2k = new DateTime.utc(2000); // 1/1/2000, UTC 16 | print(y2k); 17 | 18 | // Specify a date and time in ms since the Unix epoch. 19 | y2k = new DateTime.fromMillisecondsSinceEpoch(946684800000, isUtc: true); 20 | print(y2k); 21 | 22 | // Parse an ISO 8601 date. 23 | y2k = DateTime.parse('2000-01-01T00:00:00Z'); 24 | print(y2k); 25 | 26 | // 1/1/2000, UTC 27 | y2k = new DateTime.utc(2000); 28 | assert(y2k.millisecondsSinceEpoch == 946684800000); 29 | 30 | // 1/1/1970, UTC 31 | var unixEpoch = new DateTime.utc(1970); 32 | assert(unixEpoch.millisecondsSinceEpoch == 0); 33 | 34 | // Add one year. 35 | var y2001 = y2k.add(const Duration(days: 366)); 36 | assert(y2001.year == 2001); 37 | 38 | // Subtract 30 days. 39 | var december2000 = y2001.subtract(const Duration(days: 30)); 40 | assert(december2000.year == 2000); 41 | assert(december2000.month == 12); 42 | 43 | // Calculate the difference between two dates. 44 | // Returns a Duration object. 45 | var duration = y2001.difference(y2k); 46 | assert(duration.inDays == 366); // y2k was a leap year. 47 | } 48 | -------------------------------------------------------------------------------- /code/library-tour/set-tests/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | getAndPutItems(); 3 | checkForItems(); 4 | intersectAndSuperset(); 5 | } 6 | 7 | void getAndPutItems() { 8 | var ingredients = new Set(); 9 | ingredients.addAll(['gold', 'titanium', 'xenon']); 10 | assert(ingredients.length == 3); 11 | 12 | // Adding a duplicate item has no effect. 13 | ingredients.add('gold'); 14 | assert(ingredients.length == 3); 15 | 16 | // Remove an item from a set. 17 | ingredients.remove('gold'); 18 | assert(ingredients.length == 2); 19 | } 20 | 21 | void checkForItems() { 22 | var ingredients = new Set(); 23 | ingredients.addAll(['gold', 'titanium', 'xenon']); 24 | 25 | // Check whether an item is in the set. 26 | assert(ingredients.contains('titanium')); 27 | 28 | // Check whether all the items are in the set. 29 | assert(ingredients.containsAll(['titanium', 'xenon'])); 30 | } 31 | 32 | void intersectAndSuperset() { 33 | var ingredients = new Set(); 34 | ingredients.addAll(['gold', 'titanium', 'xenon']); 35 | 36 | // Create the intersection of two sets. 37 | var nobleGases = new Set.from(['xenon', 'argon']); 38 | var intersection = ingredients.intersection(nobleGases); 39 | assert(intersection.length == 1); 40 | assert(intersection.contains('xenon')); 41 | 42 | // Check whether one set is a superset of another collection. 43 | var allElements = [ 44 | 'hydrogen', 45 | 'helium', 46 | 'lithium', 47 | 'beryllium', 48 | 'gold', 49 | 'titanium', 50 | 'xenon' /* all the rest */ 51 | ]; 52 | assert(allElements.toSet().containsAll(ingredients)); 53 | } 54 | -------------------------------------------------------------------------------- /code/.analysis_options: -------------------------------------------------------------------------------- 1 | # Supported lint rules and documentation: 2 | # http://dart-lang.github.io/linter/lints/ 3 | 4 | analyzer: 5 | strong-mode: true 6 | exclude: 7 | - language-tour/reference/*.dart 8 | - library-tour/reference/*.dart 9 | - library-tour/async-await/bin/main.dart 10 | 11 | linter: 12 | rules: 13 | - avoid_empty_else 14 | - cancel_subscriptions 15 | - close_sinks 16 | - comment_references 17 | - control_flow_in_finally 18 | - empty_statements 19 | - hash_and_equals 20 | - iterable_contains_unrelated_type 21 | - list_remove_unrelated_type 22 | - test_types_in_equals 23 | - throw_in_finally 24 | - unrelated_type_equality_checks 25 | - valid_regexps 26 | 27 | # - always_declare_return_types 28 | # - always_specify_types 29 | - annotate_overrides 30 | # - avoid_as 31 | - avoid_init_to_null 32 | - avoid_return_types_on_setters 33 | - await_only_futures 34 | - camel_case_types 35 | - constant_identifier_names 36 | - empty_catches 37 | - empty_constructor_bodies 38 | - implementation_imports 39 | - library_names 40 | - library_prefixes 41 | - non_constant_identifier_names 42 | - one_member_abstracts 43 | - only_throw_errors 44 | - overridden_fields 45 | - package_api_docs 46 | - package_prefixed_library_names 47 | - prefer_is_not_empty 48 | # - public_member_api_docs 49 | - slash_for_doc_comments 50 | - sort_constructors_first 51 | - sort_unnamed_constructors_first 52 | - super_goes_last 53 | - type_annotate_public_apis 54 | - type_init_formals 55 | - unnecessary_brace_in_string_interp 56 | - unnecessary_getters_setters 57 | 58 | - package_names 59 | -------------------------------------------------------------------------------- /code/library-tour/list-tests/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | createLists(); 3 | findItem(); 4 | sortList(); 5 | useTypes(); 6 | } 7 | 8 | void useTypes() { 9 | // This list should contain only strings. 10 | var fruits = new List(); 11 | 12 | fruits.add('apples'); 13 | var fruit = fruits[0]; 14 | assert(fruit is String); 15 | 16 | try { 17 | // Generates static analysis warning, num is not a string. 18 | // fruits.add(5); // BAD: Throws exception in checked mode. 19 | } on Error catch (e) { 20 | print('Caught exception, as expected in checked mode.'); 21 | print(' $e'); 22 | } 23 | } 24 | 25 | void sortList() { 26 | var fruits = ['bananas', 'apples', 'oranges']; 27 | 28 | // Sort a list. 29 | fruits.sort((a, b) => a.compareTo(b)); 30 | assert(fruits[0] == 'apples'); 31 | } 32 | 33 | void findItem() { 34 | var fruits = ['apples', 'oranges']; 35 | 36 | // Access a list item by index. 37 | assert(fruits[0] == 'apples'); 38 | 39 | // Find an item in a list. 40 | assert(fruits.indexOf('apples') == 0); 41 | } 42 | 43 | void createLists() { 44 | // Use a List constructor. 45 | var vegetables = new List(); 46 | 47 | // Or simply use a list literal. 48 | var fruits = ['apples', 'oranges']; 49 | 50 | // Add to a list. 51 | fruits.add('kiwis'); 52 | 53 | // Add multiple items to a list. 54 | fruits.addAll(['grapes', 'bananas']); 55 | 56 | // Get the list length. 57 | assert(fruits.length == 5); 58 | 59 | // Remove a single item. 60 | var appleIndex = fruits.indexOf('apples'); 61 | fruits.removeAt(appleIndex); 62 | assert(fruits.length == 4); 63 | 64 | // Remove all elements from a list. 65 | fruits.clear(); 66 | assert(fruits.length == 0); 67 | } 68 | -------------------------------------------------------------------------------- /code/library-tour/async-await/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | 3 | // I deleted types from functions so that runBroken would run. 4 | 5 | //Future findEntrypoint() async => 'entrypoint'; 6 | findEntrypoint() async => 'entrypoint'; 7 | 8 | //Future runExecutable(String entrypoint, List args) async { 9 | Future runExecutable(entrypoint, args) async { 10 | print('runExecutable($entrypoint, $args)'); 11 | return 0; 12 | } 13 | 14 | //Future flushThenExit(exitCode) async { 15 | flushThenExit(exitCode) async { 16 | print('flushing then exiting: $exitCode'); 17 | return 0; 18 | } 19 | 20 | // This function has bad code! It doesn't wait for the Futures 21 | // returned by findEntrypoint(), runExecutable(), and flushThenExit() 22 | // to complete. 23 | runBroken() { 24 | var args = ['runBroken']; 25 | 26 | // Here's where the trouble starts. 27 | var entrypoint = findEntrypoint(); 28 | 29 | // entrypoint is a Future, not the real entrypoint. 30 | var exitCode = runExecutable(entrypoint, args); 31 | 32 | // exitCode is a Future, not the real exit code. 33 | flushThenExit(exitCode); 34 | } 35 | 36 | //Future runUsingAsyncAwait() async { // return type of async is Future 37 | runUsingAsyncAwait() async { 38 | var args = ['runUsingAsyncAwait']; 39 | 40 | var entrypoint = await findEntrypoint(); 41 | var exitCode = await runExecutable(entrypoint, args); 42 | await flushThenExit(exitCode); 43 | } 44 | 45 | //void runUsingFuture() { 46 | runUsingFuture() { 47 | var args = ['runUsingFuture']; 48 | 49 | findEntrypoint().then((entrypoint) { 50 | return runExecutable(entrypoint, args); 51 | }).then(flushThenExit); 52 | } 53 | 54 | void main() { 55 | runBroken(); 56 | runUsingFuture(); 57 | runUsingAsyncAwait(); 58 | } 59 | -------------------------------------------------------------------------------- /code/library-tour/future-then/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Ch03 html 7 | 8 | 9 | 10 |

dart:html tests

11 | 12 | A text input field. 13 | Another text input field. 14 | 15 |

16 | This is text. 17 | And here is some 18 | link text. 19 |

20 | 21 |
22 |

FIND THIS P

23 |

AND THIS P

24 |

but not this p

25 |
26 | and not this div 27 |
28 |
29 | 30 | 31 |

32 | Words for Linux 33 | Words for Mac 34 | Words for Windows 35 |

36 | 37 |
38 |

Sometimes visible, sometimes not.

39 |
40 | 41 |
42 |
43 | Inputs go here 44 |
45 |
46 | Status goes here 47 |
48 |
49 | Some expendable text 50 |
51 |
52 | 53 |
54 |
55 | This is a message 56 |
57 |
58 | 59 |
60 | 61 |
62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /code/language-tour/flow-switch-case/bin/main.dart: -------------------------------------------------------------------------------- 1 | void executeClosed() {} 2 | void executePending() {} 3 | void executeApproved() {} 4 | void executeDenied() {} 5 | void executeOpen() {} 6 | void executeUnknown() {} 7 | void executeNowClosed() {} 8 | 9 | void main() { 10 | switcher(); 11 | switcheroo(); 12 | switcherooni(); 13 | switchItUp(); 14 | } 15 | 16 | void switcher() { 17 | var command = 'OPEN'; 18 | switch (command) { 19 | case 'CLOSED': 20 | executeClosed(); 21 | break; 22 | case 'PENDING': 23 | executePending(); 24 | break; 25 | case 'APPROVED': 26 | executeApproved(); 27 | break; 28 | case 'DENIED': 29 | executeDenied(); 30 | break; 31 | case 'OPEN': 32 | executeOpen(); 33 | break; 34 | default: 35 | executeUnknown(); 36 | } 37 | } 38 | 39 | void switcheroo() { 40 | var command = 'OPEN'; 41 | switch (command) { 42 | case 'OPEN': 43 | executeOpen(); 44 | // ERROR: Missing break causes an exception!! 45 | // NOTE: Added break so sample passes dart_analyzer 46 | break; 47 | 48 | case 'CLOSED': 49 | executeClosed(); 50 | break; 51 | } 52 | } 53 | 54 | void switcherooni() { 55 | var command = 'CLOSED'; 56 | switch (command) { 57 | case 'CLOSED': // Empty case falls through. 58 | case 'NOW_CLOSED': 59 | // Runs for both CLOSED and NOW_CLOSED. 60 | executeNowClosed(); 61 | break; 62 | } 63 | } 64 | 65 | void switchItUp() { 66 | var command = 'CLOSED'; 67 | switch (command) { 68 | case 'CLOSED': 69 | executeClosed(); 70 | continue nowClosed; 71 | // Continues executing at the nowClosed label. 72 | 73 | nowClosed: case 'NOW_CLOSED': 74 | // Runs for both CLOSED and NOW_CLOSED. 75 | executeNowClosed(); 76 | break; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /code/library-tour/get-data-from-server/web/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:html'; 2 | import 'dart:async'; 3 | import 'dart:convert' show JSON; 4 | 5 | // A JSON-formatted file next to this page. 6 | String jsonUri = 'data.json'; 7 | // Besides 'data.json', also try 'nodata.json' and 'nofile.json'. 8 | 9 | // An XML-formatted file next to this page. 10 | String xmlUri = 'data.xml'; 11 | // Besides 'data.xml', also try 'nodata.xml' and 'nofile.xml'. 12 | 13 | Future main() async { 14 | var data; 15 | 16 | // Read a JSON file. 17 | try { 18 | data = await HttpRequest.getString(jsonUri); 19 | processString(data); 20 | } catch (e) { 21 | handleError(e); 22 | } 23 | 24 | // Read an XML file. 25 | try { 26 | data = await HttpRequest.request(xmlUri); 27 | processRequest(data); 28 | } catch (e) { 29 | handleError(e); 30 | } 31 | } 32 | 33 | void processString(String jsonText) { 34 | parseText(jsonText); 35 | } 36 | 37 | void processRequest(HttpRequest request) { 38 | var xmlDoc = request.responseXml; 39 | try { 40 | var license = xmlDoc.querySelector('license').text; 41 | print('License: $license'); 42 | } catch (e) { 43 | print("$xmlUri doesn't have correct XML formatting."); 44 | } 45 | } 46 | 47 | void parseText(String jsonText) { 48 | var response; 49 | 50 | try { 51 | response = JSON.decode(jsonText); 52 | } on FormatException catch (e) { 53 | print("$jsonUri doesn't have correct JSON formatting."); 54 | print(e.message); 55 | return; 56 | } 57 | 58 | String name = response['name']; 59 | String license = response['license']; 60 | print('The $name project uses the $license license.'); 61 | } 62 | 63 | // PENDING: We should do more explicit error handling, once it's available. 64 | void handleError(int error) { 65 | print('Uh oh, there was an error.'); 66 | print(error.toString()); 67 | } 68 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Except as otherwise noted, the content of this page is licensed under the 2 | Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States 3 | License [1], and code samples are licensed 4 | under the BSD License: 5 | 6 | Copyright 2012, the Dart project authors. All rights reserved. 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are 9 | met: 10 | * Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | * Redistributions in binary form must reproduce the above 13 | copyright notice, this list of conditions and the following 14 | disclaimer in the documentation and/or other materials provided 15 | with the distribution. 16 | * Neither the name of Google Inc. nor the names of its 17 | contributors may be used to endorse or promote products derived 18 | from this software without specific prior written permission. 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | [1] http://creativecommons.org/licenses/by-nc-nd/3.0/us/ 32 | -------------------------------------------------------------------------------- /code/language-tour/object-classes/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math' show sqrt; 2 | import 'dart:convert' show JSON; 3 | 4 | class Point { 5 | num x; 6 | num y; 7 | 8 | // Syntactic sugar for setting x and y 9 | // before the constructor body runs. 10 | Point(this.x, this.y); 11 | 12 | // Initializer list sets instance variables before the constructor body runs. 13 | Point.fromJson(Map jsonMap) 14 | : x = jsonMap['x'], 15 | y = jsonMap['y'] { 16 | print('In Point.fromJson(): ($x, $y)'); 17 | } 18 | 19 | num distanceTo(Point other) { 20 | var dx = x - other.x; 21 | var dy = y - other.y; 22 | return sqrt(dx * dx + dy * dy); 23 | } 24 | } 25 | 26 | class ImmutablePoint { 27 | final num x; 28 | final num y; 29 | const ImmutablePoint(this.x, this.y); 30 | static final ImmutablePoint origin = const ImmutablePoint(0, 0); 31 | } 32 | 33 | void main() { 34 | var jsonData = JSON.decode('{"x":1, "y":2}'); 35 | 36 | // Create a Point using Point(). 37 | var p1 = new Point(2, 2); 38 | 39 | // Create a Point using Point.fromJson(). 40 | var p2 = new Point.fromJson(jsonData); 41 | 42 | var p = new Point(2, 2); 43 | 44 | // Set the value of the instance variable y. 45 | p.y = 3; 46 | 47 | // Get the value of y. 48 | assert(p.y == 3); 49 | 50 | // Invoke distanceTo() on p. 51 | num distance = p.distanceTo(new Point(4, 4)); 52 | 53 | /* 54 | * TODO: make this testable 55 | query('#button') 56 | ..text = 'Click to Confirm' // Get an object. Use its 57 | ..classes.add('important') // instance variables 58 | ..onClick.add((e) => window.alert('Confirmed!')); // and methods. 59 | */ 60 | 61 | /* var */ p = const ImmutablePoint(2, 2) as Point; 62 | var a = const ImmutablePoint(1, 1); 63 | var b = const ImmutablePoint(1, 1); 64 | 65 | assert(identical(a, b)); // They are the same instance! 66 | 67 | print('The type of a is ${a.runtimeType}'); 68 | } 69 | -------------------------------------------------------------------------------- /code/language-tour/flow/exceptions/web/main.dart: -------------------------------------------------------------------------------- 1 | class OutOfLlamasException extends Error {} 2 | 3 | void breedMoreLlamas() {} 4 | void buyMoreLlamas() { 5 | print('bought more llamas'); 6 | } 7 | 8 | void cleanLlamaStalls() {} 9 | 10 | void breedYetMoreLlamas() { 11 | throw new OutOfLlamasException(); 12 | // throw new Exception('No more llamas!'); 13 | // throw 'Not even an exception'; 14 | } 15 | 16 | class Point {} 17 | 18 | void main() { 19 | var numberOfLlamas = 1; 20 | if (numberOfLlamas <= 0) { 21 | throw new StateError('Value must be greater than zero'); 22 | } 23 | if (numberOfLlamas <= 0) { 24 | throw new StateError('Out of llamas!'); 25 | } 26 | 27 | void distanceTo(Point other) => throw new UnimplementedError(); 28 | 29 | try { 30 | breedMoreLlamas(); 31 | } on OutOfLlamasException { 32 | buyMoreLlamas(); 33 | } 34 | 35 | try { 36 | breedMoreLlamas(); 37 | } on OutOfLlamasException { 38 | // A specific exception 39 | buyMoreLlamas(); 40 | } on Exception catch (e) { 41 | // Anything else that is an exception 42 | print('Unknown exception: $e'); 43 | } catch (e) { 44 | // No specified type, handles all 45 | print('Something really unknown: $e'); 46 | } 47 | 48 | try { 49 | breedMoreLlamas(); 50 | } finally { 51 | // Always clean up, even if an exception is thrown. 52 | cleanLlamaStalls(); 53 | } 54 | 55 | try { 56 | breedMoreLlamas(); 57 | } catch (e) { 58 | print('Error: $e'); // Handle the exception first. 59 | } finally { 60 | cleanLlamaStalls(); // Then clean up. 61 | } 62 | 63 | try { 64 | throw new FormatException('Expected at least 1 section'); 65 | } on FormatException catch (e) { 66 | print(e); 67 | } 68 | 69 | try { 70 | print('About to breed too many llamas.'); 71 | breedYetMoreLlamas(); 72 | } on OutOfLlamasException { 73 | buyMoreLlamas(); 74 | } on Exception catch (e) { 75 | print('Exception details:\n $e'); 76 | } catch (e, s) { 77 | print('Exception details:\n $e'); 78 | print('Stack trace:\n $s'); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /code/library-tour/futures/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | 3 | // fake methods to make the below examples work 4 | 5 | Future getFuture() async { 6 | return 0; 7 | } 8 | 9 | void handleValue(int value) { 10 | print('value is $value'); 11 | } 12 | 13 | void handleError(int error) { 14 | print('error is $error'); 15 | } 16 | 17 | Future dbQuery(String query) async { 18 | return true; 19 | } 20 | 21 | Future expensiveWork() async { 22 | return true; 23 | } 24 | 25 | Future lengthyComputation() async { 26 | return true; 27 | } 28 | 29 | Future costlyQuery() async { 30 | // Uncomment the throw and comment the return to see error handling. 31 | // throw 'costlyQuery failed!!!'; 32 | return true; 33 | } 34 | 35 | Future deleteLotsOfFiles() async { 36 | return true; 37 | } 38 | 39 | Future copyLotsOfFiles() async { 40 | return true; 41 | } 42 | 43 | Future checksumLotsOfOtherFiles() async { 44 | return true; 45 | } 46 | 47 | // end fake methods 48 | 49 | Future longExpensiveSearch() { 50 | // Do some setup. 51 | // Perform an exhaustive search. 52 | return dbQuery('select * from table'); 53 | } 54 | 55 | Future runSearch() async { 56 | try { 57 | var results = await longExpensiveSearch(); 58 | print('Here are the results: $results'); 59 | } catch (e) { 60 | print("Oops! Encountered $e"); 61 | } 62 | } 63 | 64 | Future runQuery() async { 65 | try { 66 | await costlyQuery(); 67 | await expensiveWork(); 68 | await lengthyComputation(); 69 | print('done!'); 70 | } catch (_) { 71 | print('DOH!'); 72 | } 73 | } 74 | 75 | Future wait() async { 76 | Future deleteDone = deleteLotsOfFiles(); 77 | Future copyDone = copyLotsOfFiles(); 78 | Future checksumDone = checksumLotsOfOtherFiles(); 79 | 80 | await Future.wait([deleteDone, copyDone, checksumDone]); 81 | print('Done with all the long steps'); 82 | } 83 | 84 | Future main() async { 85 | Future future = getFuture(); 86 | var value = await future; 87 | try { 88 | handleValue(value); 89 | } catch (e) { 90 | handleError(e); 91 | } 92 | 93 | runSearch(); 94 | runQuery(); 95 | wait(); 96 | } 97 | -------------------------------------------------------------------------------- /runtests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #### 4 | # TODO: Should run DumpRenderTree, but what to grep for? 5 | # 6 | # DumpRenderTree ch03/ch03_html/web/ch03_html.html 7 | # DumpRenderTree ch03/ch03_2_html/web/ch03_2_html.html 8 | 9 | EXITSTATUS=0 10 | PASSING=0 11 | WARNINGS=0 12 | FAILURES=0 13 | 14 | ##### 15 | # Type Analysis 16 | 17 | ANA="dartanalyzer" 18 | 19 | echo "Type Analysis, running dartanalyzer..." 20 | 21 | EXITSTATUS=0 22 | 23 | #### 24 | # test library import samples 25 | # 26 | for file in using_libraries.dart using_schemes.dart library_partial.dart library_prefix.dart 27 | do 28 | (cd code/ch02/libraries; results=`$ANA $file 2>&1`) 29 | echo "" 30 | if [ -n "$results" ]; then 31 | EXITSTATUS=1 32 | echo "$results" 33 | echo "$file: FAILURE." 34 | else 35 | echo "$file: Passed analysis." 36 | fi 37 | done 38 | 39 | 40 | #### 41 | # test ballgame files as a unit 42 | # 43 | results=`$ANA code/ch02/ballgame/ballgame.dart code/ch02/ballgame/ball.dart code/ch02/ballgame/util.dart 2>&1` 44 | exit_code=$? 45 | echo "" 46 | if [ $exit_code -eq 2 ]; then 47 | let FAILURES++ 48 | EXITSTATUS=1 49 | echo "$results" 50 | echo "$file: FAILURE." 51 | elif [ $exit_code -eq 1 ]; then 52 | let WARNINGS++ 53 | echo "$results" 54 | echo "$file: WARNING." 55 | elif [ $exit_code -eq 0 ]; then 56 | let PASSING++ 57 | echo "$results" 58 | else 59 | echo "$file: Unknown exit code: $exit_code." 60 | fi 61 | 62 | #### 63 | # test all other files one at a time 64 | # 65 | for file in code/ch0*/*.dart code/ch03/*html/web/*.dart 66 | do 67 | echo "" 68 | results=`$ANA $file 2>&1` 69 | exit_code=$? 70 | if [ $exit_code -eq 2 ]; then 71 | let FAILURES++ 72 | EXITSTATUS=1 73 | echo "$results" 74 | echo "$file: FAILURE." 75 | elif [ $exit_code -eq 1 ]; then 76 | let WARNINGS++ 77 | echo "$results" 78 | echo "$file: WARNING." 79 | elif [ $exit_code -eq 0 ]; then 80 | let PASSING++ 81 | echo "$results" 82 | else 83 | echo "$file: Unknown exit code: $exit_code." 84 | fi 85 | done 86 | 87 | echo 88 | echo "####################################################" 89 | echo "PASSING = $PASSING" 90 | echo "WARNINGS = $WARNINGS" 91 | echo "FAILURES = $FAILURES" 92 | echo "####################################################" 93 | echo 94 | exit $EXITSTATUS 95 | -------------------------------------------------------------------------------- /archive/foreword.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | Lars 10 | 11 | Bak 12 | 13 | 14 | 15 | Kasper 16 | 17 | Lund 18 | 19 | 20 | 21 | Designers of the Dart programming language, October 22 | 2012 23 | 24 | 25 | October 2012 26 | 27 | 28 | Foreword 29 | 30 | When we joined Google and entered the fascinating world of web browser 31 | development more than six years ago, the web was a different place. It was 32 | clear that a new breed of web apps was emerging, but the performance of the 33 | underlying platform left much to be desired. Given our background in 34 | designing and implementing virtual machines, building a high performance 35 | JavaScript engine seemed like an interesting challenge. It was. We 36 | implemented the V8 JavaScript engine from scratch and shipped it as part of 37 | Google Chrome in 2008, and we are very proud of the positive performance 38 | impact our work seems to have had on the entire browser industry. 39 | 40 | Even though recent performance gains in web browsers have shattered 41 | most limits on how large and complex web apps can be, building large, 42 | high-performance web apps remains hard. Without good abstraction mechanisms 43 | and clean semantics, developers often end up with complex and convoluted 44 | code. Naturally, this problem gets exacerbated as the codebase grows. We 45 | designed the Dart programming language to solve this exact problem, and we 46 | hope that programmers will be more productive as a result. 47 | 48 | It is very satisfying to see how Dart inspires programmers to strive 49 | for concise, elegant programs. There is something very enjoyable about 50 | incrementally transforming prototypes into maintainable production software 51 | through refactorings and adding type annotations—and it definitely feels 52 | like Dart as a language scales well from small experiments to large projects 53 | with lots of code. 54 | 55 | Dart: Up and Running is a practical guide that 56 | introduces the Dart programming language and teaches you how to build Dart 57 | applications. We hope you will enjoy the book and Dart. 58 | 59 | -------------------------------------------------------------------------------- /code/library-tour/map-1/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | buildMaps(); 3 | addGetSetItems(); 4 | getMapValues(); 5 | iterateThroughPairs(); 6 | checkForKey(); 7 | callPutIfAbsent(); 8 | } 9 | 10 | void buildMaps() { 11 | // Maps often use strings as keys. 12 | var hawaiianBeaches = { 13 | 'Oahu': ['Waikiki', 'Kailua', 'Waimanalo'], 14 | 'Big Island': ['Wailea Bay', 'Pololu Beach'], 15 | 'Kauai': ['Hanalei', 'Poipu'] 16 | }; 17 | 18 | // Maps can be built from a constructor. 19 | var searchTerms = new Map(); 20 | 21 | // Maps are parameterized types; you can specify what 22 | // types the key and value should be. 23 | var nobleGases = new Map(); 24 | } 25 | 26 | void addGetSetItems() { 27 | var nobleGases = {54: 'xenon'}; 28 | 29 | // Retrieve a value with a key. 30 | assert(nobleGases[54] == 'xenon'); 31 | 32 | // Check whether a map contains a key. 33 | assert(nobleGases.containsKey(54)); 34 | 35 | // Remove a key and its value. 36 | nobleGases.remove(54); 37 | assert(!nobleGases.containsKey(54)); 38 | } 39 | 40 | void getMapValues() { 41 | var hawaiianBeaches = { 42 | 'Oahu': ['Waikiki', 'Kailua', 'Waimanalo'], 43 | 'Big Island': ['Wailea Bay', 'Pololu Beach'], 44 | 'Kauai': ['Hanalei', 'Poipu'] 45 | }; 46 | 47 | // Get all the keys as an unordered collection 48 | // (an Iterable). 49 | var keys = hawaiianBeaches.keys; 50 | 51 | assert(keys.length == 3); 52 | assert(new Set.from(keys).contains('Oahu')); 53 | 54 | // Get all the values as an unordered collection 55 | // (an Iterable of Lists). 56 | var values = hawaiianBeaches.values; 57 | assert(values.length == 3); 58 | assert(values.any((v) => v.contains('Waikiki'))); 59 | } 60 | 61 | void iterateThroughPairs() { 62 | var hawaiianBeaches = { 63 | 'Oahu': ['Waikiki', 'Kailua', 'Waimanalo'], 64 | 'Big Island': ['Wailea Bay', 'Pololu Beach'], 65 | 'Kauai': ['Hanalei', 'Poipu'] 66 | }; 67 | 68 | // NOTE: Do not depend on iteration order. 69 | hawaiianBeaches.forEach((k, v) { 70 | print('I want to visit $k and swim at $v'); 71 | // I want to visit Oahu and swim at 72 | // [Waikiki, Kailua, Waimanalo], etc. 73 | }); 74 | } 75 | 76 | void checkForKey() { 77 | var hawaiianBeaches = { 78 | 'Oahu': ['Waikiki', 'Kailua', 'Waimanalo'], 79 | 'Big Island': ['Wailea Bay', 'Pololu Beach'], 80 | 'Kauai': ['Hanalei', 'Poipu'] 81 | }; 82 | 83 | assert(hawaiianBeaches.containsKey('Oahu')); 84 | assert(!hawaiianBeaches.containsKey('Florida')); 85 | } 86 | 87 | String pickToughestKid() => "Cosmo"; 88 | 89 | void callPutIfAbsent() { 90 | var teamAssignments = {}; 91 | teamAssignments.putIfAbsent('Catcher', () => pickToughestKid()); 92 | assert(teamAssignments['Catcher'] != null); 93 | teamAssignments.forEach((k, v) { 94 | print('$k: $v'); 95 | }); 96 | } 97 | -------------------------------------------------------------------------------- /code/library-tour/mirrors/bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:mirrors'; 2 | 3 | String askUserForNameOfFunction() => 'foo'; 4 | 5 | class Person { 6 | String firstName; 7 | String lastName; 8 | int age; 9 | 10 | Person(this.firstName, this.lastName, this.age); 11 | 12 | String get fullName => '$firstName $lastName'; 13 | 14 | void greet(String other) { 15 | print('Hello there, $other!'); 16 | } 17 | } 18 | 19 | void main() { 20 | // If the symbol name is known at compile time. 21 | const className = #MyClass; 22 | 23 | // If the symbol name is dynamically determined. 24 | var userInput = askUserForNameOfFunction(); 25 | var functionName = new Symbol(userInput); 26 | 27 | print('className = ${MirrorSystem.getName(className)}'); 28 | assert('MyClass' == MirrorSystem.getName(className)); 29 | assert('MyClass' == MirrorSystem.getName(#MyClass)); 30 | 31 | ClassMirror mirror = reflectClass(Person); 32 | assert('Person' == MirrorSystem.getName(mirror.simpleName)); 33 | 34 | reflectFromInstance(); 35 | showConstructors(mirror); 36 | showFields(mirror); 37 | reflectOnInstance(); 38 | } 39 | 40 | void reflectFromInstance() { 41 | var person = new Person('Bob', 'Smith', 33); 42 | ClassMirror mirror = reflectClass(person.runtimeType); 43 | assert('Person' == MirrorSystem.getName(mirror.simpleName)); 44 | } 45 | 46 | void showConstructors(ClassMirror mirror) { 47 | var constructors = mirror.declarations.values 48 | .where((m) => m is MethodMirror && m.isConstructor); 49 | 50 | constructors.forEach((m) { 51 | MethodMirror mm = m as MethodMirror; 52 | print('The constructor ${mm.simpleName} has ' 53 | '${mm.parameters.length} parameters.'); 54 | }); 55 | } 56 | 57 | void showFields(ClassMirror mirror) { 58 | var fields = mirror.declarations.values.where((m) => m is VariableMirror); 59 | 60 | fields.forEach((m) { 61 | VariableMirror v = m as VariableMirror; 62 | var finalStatus = v.isFinal ? 'final' : 'not final'; 63 | var privateStatus = v.isPrivate ? 'private' : 'not private'; 64 | var typeAnnotation = v.type.simpleName; 65 | 66 | print('The field ${v.simpleName} is $privateStatus ' + 67 | 'and $finalStatus and is annotated as ' + 68 | '$typeAnnotation.'); 69 | }); 70 | } 71 | 72 | void reflectOnInstance() { 73 | var p = new Person('Bob', 'Smith', 42); 74 | InstanceMirror mirror = reflect(p); 75 | 76 | // Get the object that the mirror reflects. 77 | var person = mirror.reflectee; 78 | assert(identical(p, person)); 79 | 80 | // Invoke a method on the object. 81 | mirror.invoke(#greet, ['Sundar']); 82 | 83 | // Get the value of a property. 84 | var fullName = mirror.getField(#fullName).reflectee; 85 | assert(fullName == 'Bob Smith'); 86 | 87 | // Set the value of a property. 88 | mirror.setField(#firstName, 'Mary'); 89 | assert(p.firstName == 'Mary'); 90 | assert(p.fullName == 'Mary Smith'); 91 | } 92 | -------------------------------------------------------------------------------- /code/library-tour/string-tests/bin/main.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | //BEGIN 3 | // Check whether a string contains another string. 4 | assert('Never odd or even'.contains('odd')); 5 | 6 | // Does a string start with another string? 7 | assert('Never odd or even'.startsWith('Never')); 8 | 9 | // Does a string end with another string? 10 | assert('Never odd or even'.endsWith('even')); 11 | 12 | // Find the location of a string inside a string. 13 | assert('Never odd or even'.indexOf('odd') == 6); 14 | //END 15 | 16 | //BEGIN 17 | // Grab a substring. 18 | assert('Never odd or even'.substring(6, 9) == 'odd'); 19 | 20 | // Split a string using a string pattern. 21 | var parts = 'structured web apps'.split(' '); 22 | assert(parts.length == 3); 23 | assert(parts[0] == 'structured'); 24 | 25 | // Get a UTF-16 code unit (as a string) by index. 26 | assert('Never odd or even'[0] == 'N'); 27 | 28 | // Use split() with an empty string parameter to get 29 | // a list of all characters (as Strings); good for 30 | // iterating. 31 | for (var char in 'hello'.split('')) { 32 | print(char); 33 | } 34 | 35 | // Get all the UTF-16 code units in the string. 36 | var codeUnitList = 'Never odd or even'.codeUnits.toList(); 37 | assert(codeUnitList[0] == 78); 38 | //END 39 | 40 | // UNUSED. 41 | // Get all the characters in the string as a Runes object, 42 | // which contains one item per character. 43 | var runes = 'Never odd or even'.runes; 44 | assert(runes.length == 17); 45 | 46 | //BEGIN 47 | // Convert to uppercase. 48 | assert('structured web apps'.toUpperCase() == 'STRUCTURED WEB APPS'); 49 | 50 | // Convert to lowercase. 51 | assert('STRUCTURED WEB APPS'.toLowerCase() == 'structured web apps'); 52 | //END 53 | 54 | //BEGIN 55 | // Trim a string. 56 | assert(' hello '.trim() == 'hello'); 57 | 58 | // Check whether a string is empty. 59 | assert(''.isEmpty); 60 | 61 | // Strings with only white space are not empty. 62 | assert(' '.isNotEmpty); 63 | //END 64 | 65 | //BEGIN 66 | var greetingTemplate = 'Hello, NAME!'; 67 | var greeting = greetingTemplate.replaceAll(new RegExp('NAME'), 'Bob'); 68 | 69 | assert(greeting != greetingTemplate); // greetingTemplate didn't change. 70 | //END 71 | 72 | buildingAString(); 73 | regularExpressions(); 74 | regularExpressions2(); 75 | } 76 | 77 | void buildingAString() { 78 | //NOTE: DID NOT USE A METHOD CASCADE. BROKE due to add return type changing. 79 | //Then add and addAll were deprecated in favor of write and writeAll, 80 | //and clear() was removed. 81 | //BEGIN 82 | var sb = new StringBuffer(); 83 | sb 84 | ..write('Use a StringBuffer for ') 85 | ..writeAll(['efficient', 'string', 'creation'], ' ') 86 | ..write('.'); 87 | 88 | var fullString = sb.toString(); 89 | 90 | assert(fullString == 'Use a StringBuffer for efficient string creation.'); 91 | //END 92 | 93 | // clear() is gone. For now, at least. 94 | // sb.clear(); // All gone! 95 | // assert(sb.toString() == ''); 96 | } 97 | 98 | void regularExpressions() { 99 | // Here's a regular expression for one or more digits. 100 | var numbers = new RegExp(r'\d+'); 101 | //NOTE: WAS: var numbers = const RegExp(r'\d+'); 102 | 103 | var allCharacters = 'llamas live fifteen to twenty years'; 104 | var someDigits = 'llamas live 15 to 20 years'; 105 | 106 | // contains() can use a regular expression. 107 | assert(!allCharacters.contains(numbers)); 108 | assert(someDigits.contains(numbers)); 109 | 110 | // Replace every match with another string. 111 | var exedOut = someDigits.replaceAll(numbers, 'XX'); 112 | assert(exedOut == 'llamas live XX to XX years'); 113 | } 114 | 115 | void regularExpressions2() { 116 | var numbers = new RegExp(r'\d+'); 117 | //NOTE: WAS: var numbers = const RegExp(r'\d+'); 118 | var someDigits = 'llamas live 15 to 20 years'; 119 | 120 | // Check whether the reg exp has a match in a string. 121 | assert(numbers.hasMatch(someDigits)); 122 | 123 | // Loop through all matches. 124 | for (var match in numbers.allMatches(someDigits)) { 125 | print(match.group(0)); // 15, then 20 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /code/library-tour/future-then/web/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:html'; 2 | 3 | void main() { 4 | findElements(); 5 | replaceElement(); 6 | displayConditionally(); 7 | setAttribute(); 8 | createElements(); 9 | funWithNodes(); 10 | funWithCss(); 11 | handleEvents(); 12 | } 13 | 14 | void deleteMe(Event event) { 15 | var text = querySelector("#sample_text_id").text; 16 | var buffer = new StringBuffer(); 17 | for (int i = text.length - 1; i >= 0; i--) { 18 | buffer.write(text[i]); 19 | } 20 | querySelector("#sample_text_id").text = buffer.toString(); 21 | } 22 | 23 | void findElements() { 24 | // Find an element by id (an-id). 25 | Element elem1 = querySelector('#an-id'); 26 | 27 | // Find an element by class (a-class). 28 | Element elem2 = querySelector('.a-class'); 29 | 30 | // Find all elements by tag (
). 31 | List elems1 = querySelectorAll('div'); 32 | 33 | // Find all text inputs. 34 | List elems2 = querySelectorAll('input[type="text"]'); 35 | 36 | // Find all elements with the CSS class 'class' 37 | // inside of a

that is inside an element with 38 | // the ID 'id'. 39 | List elems3 = querySelectorAll('#id p.class'); 40 | } 41 | 42 | void replaceElement() { 43 | var elem = querySelector('#example') as AnchorElement; 44 | elem.href = 'http://dartlang.org'; 45 | } 46 | 47 | void displayConditionally() { 48 | // In Dart: 49 | final osList = ['macos', 'windows', 'linux']; 50 | 51 | // In real code you'd programmatically determine userOs. 52 | var userOs = 'linux'; 53 | 54 | for (var os in osList) { 55 | // For each possible OS... 56 | bool shouldShow = (os == userOs); // Matches user OS? 57 | 58 | // Find all elements with class=os. For example, if 59 | // os == 'windows', call querySelectorAll('.windows') 60 | // to find all elements with the class "windows". 61 | // Note that '.$os' uses string interpolation. 62 | for (var elem in querySelectorAll('.$os')) { 63 | elem.hidden = !shouldShow; // Show or hide. 64 | } 65 | } 66 | } 67 | 68 | void setAttribute() { 69 | Element elem = querySelector('#shy'); 70 | 71 | // Set a non-existent attribute. No error checking/complaints! 72 | elem.attributes['someAttribute'] = 'someValue'; 73 | 74 | elem.attributes['hidden'] = 'true'; 75 | // elem.hidden = true; // BETTER! 76 | } 77 | 78 | void createElements() { 79 | var elem = new ParagraphElement(); 80 | elem.text = 'Creating is easy!'; 81 | document.body.children.add(elem); 82 | 83 | var elem2 = new Element.html('

Creating is easy!

'); 84 | document.body.children.add(elem2); 85 | } 86 | 87 | void funWithNodes() { 88 | var elem = new Element.html('

Some styled text.

'); 89 | 90 | // Find the parent by ID, and add elem as its last child. 91 | querySelector('#inputs').nodes.add(elem); 92 | 93 | // Find a node by ID, and replace it in the DOM. 94 | querySelector('#status').replaceWith(elem); 95 | 96 | // Find a node by ID, and remove it from the DOM. 97 | querySelector('#expendable').remove(); 98 | } 99 | 100 | void funWithCss() { 101 | var element = querySelector('#message'); 102 | element.classes.add('warning'); 103 | // 104 | // var message = new DivElement(); 105 | // message.id = 'message2'; 106 | // message.text = 'Please subscribe to the Dart mailing list.'; 107 | 108 | var message = new DivElement() 109 | ..id = 'message2' 110 | ..text = 'Please subscribe to the Dart mailing list.'; 111 | 112 | message.style 113 | ..fontWeight = 'bold' 114 | ..fontSize = '2em'; 115 | 116 | querySelector("#fun-with-css").children.add(message); 117 | } 118 | 119 | void handleEvents() { 120 | // Find a button by ID and add an event handler. 121 | querySelector('#submitInfo').onClick.listen((_) { 122 | // When the button is clicked, it runs this code. 123 | submitData(); 124 | }); 125 | 126 | document.body.onClick.listen((e) { 127 | var clickedElement = e.target as ButtonElement; 128 | var id = clickedElement.id; 129 | var description; 130 | 131 | if (id != null && id.length > 0) { 132 | description = 'the "$id"'; 133 | } else { 134 | description = 'a <${clickedElement.tagName}>'; 135 | } 136 | 137 | print('You clicked $description element.'); 138 | }); 139 | } 140 | 141 | void submitData() { 142 | print('data submitted'); 143 | } 144 | -------------------------------------------------------------------------------- /archive/bookinfo.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Christopher 7 | 8 | Hearse 9 | 10 | 11 | 12 | David 13 | 14 | Futato 15 | 16 | 17 | 18 | Rebecca 19 | 20 | Demarest 21 | 22 | 23 | 24 | Randy 25 | 26 | Comer 27 | 28 | 29 | 30 | 31 | 32 | O’Reilly Media, Inc. 33 | 34 |
35 | 1005 Gravenstein Highway North 36 | Sebastopol 37 | CA 38 | 95472 39 |
40 |
41 | 42 | 43 | Printed in the United States of America. 44 | 45 | 46 | 47 | [LSI] 48 | 49 | 50 | 51 | O’Reilly books may be purchased for educational, business, or sales 52 | promotional use. Online editions are also available for most titles 53 | (http://my.safaribooksonline.com). 55 | For more information, contact our corporate/institutional sales 56 | department: 800-998-9938 or corporate@oreilly.com. 57 | 58 | 59 | 60 | Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo 61 | are registered trademarks of O’Reilly Media, Inc. Dart: Up and 62 | Running, the image of a greater roadrunner, and related trade 63 | dress are trademarks of O’Reilly Media, Inc. 64 | 65 | This text of this work is available at this book’s GitHub project 66 | (https://github.com/dart-lang/dart-up-and-running-book) under the Creative 67 | Commons Attribution-Noncommercial-No Derivative Works 3.0 United States 68 | License (http://creativecommons.org/licenses/by-nc-nd/3.0/us/). 69 | 70 | Many of the designations used by manufacturers and sellers to 71 | distinguish their products are claimed as trademarks. Where those 72 | designations appear in this book, and O’Reilly Media, Inc., was aware of a 73 | trademark claim, the designations have been printed in caps or initial 74 | caps. 75 | 76 | 77 | 78 | While every precaution has been taken in the preparation of this 79 | book, the publisher and authors assume no responsibility for errors or 80 | omissions, or for damages resulting from the use of the information 81 | contained herein. 82 | 83 | 84 | 9781449330897 85 | 86 | 1 87 | 88 | 89 | Kathy 90 | 91 | Walrath 92 | 93 | 94 | Kathy is a technical writer who’s worked on docs for Chrome and 95 | other developer APIs at Google since 2006. Before that, she worked at 96 | Sun, NeXT, and HP. Back when the Web was young, she wrote the first doc 97 | to help developers write Java applets. She also co-created The 98 | Java Tutorial and maintained it for a very long time. 99 | 100 | 101 | 102 | 103 | Seth 104 | 105 | Ladd 106 | 107 | 108 | Seth is a Developer Advocate with the Chrome team. He is a 109 | conference organizer (Aloha on 110 | Rails, New Game) and author (Expert Spring 111 | MVC [Apress]), helped publish Angry Birds for the Web, and is 112 | a big fan of HTML5 and the modern Web. 113 | 114 | 115 | 116 | 117 | Meghan 118 | 119 | Blanchette 120 | 121 | 122 | 123 | 2013 124 | 125 | Kathy Walrath, Seth Ladd 126 | 127 | 128 | 129 | Christopher 130 | 131 | Hearse 132 | 133 | 134 | 135 | 136 | First Edition 137 | 138 | October, 2012 139 | 140 | 141 | 142 | 143 | 144 | 2012-10-24 145 | 146 | First release 147 | 148 | 149 | 150 | 2013-03-29 151 | 152 | Second release 153 | 154 | 155 | 156 | 157 | 158 | 159 |
-------------------------------------------------------------------------------- /archive/ch01.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | Quick Start 8 | 9 | Welcome to Dart, an open-source, batteries-included developer platform 10 | for building structured HTML5 web apps. This chapter tells you why Google 11 | created Dart, what’s cool about Dart, and how to write and run your first 12 | Dart app. 13 | 14 | Dart provides not only a new language, but libraries, an editor, a 15 | virtual machine (VM), a browser that can run Dart apps natively, and a 16 | compiler to JavaScript. Dart aims to be a more productive way to build the 17 | high-performance, modern apps that users demand. 18 | 19 | 20 | Why Google Created Dart 21 | 22 | Google cares a lot about helping to make the web great. We write a 23 | lot of web apps, many of them quite sophisticated—think Gmail, Google 24 | Calendar, Google+, and more. We want web apps to load quickly, run 25 | smoothly, and present engaging and fun experiences to users. We want 26 | developers of all backgrounds to be able to build great experiences for 27 | the browser. 28 | 29 | As an example of Google’s commitment to the web, consider the Google 30 | Chrome browser. Google created it to spur competition at a time when the 31 | web platform seemed to be stagnating. It worked. As shows, browser speed has increased immensely 33 | since Chrome’s introduction in 2008. 34 | 35 | 36 | The JavaScript engine known as V8 is 37 | responsible for much of Chrome’s speed. Many of the V8 engineers are now 38 | working on the Dart project. 39 | 40 | 41 |
42 | Browser speed (V8 benchmark suite v7; higher numbers are 43 | better) 44 | 45 | 46 | 47 | 48 | 49 | 50 |
51 | 52 | The number of new features in browsers has also increased, with APIs 53 | such as WebGL, FileSystem, Web workers, and WebSockets. Browsers now have 54 | automatic update capabilities, frequently delivering new features and 55 | fixes directly to the user. Mobile devices such as tablets and phones also 56 | have modern browsers with many HTML5 features. 57 | 58 | Despite these improvements in the web platform, the developer 59 | experience hasn’t improved as much as we’d like. We believe it should be 60 | easier to build larger, more complex web apps. It’s taken far too long for 61 | productive tools to emerge, and they still don’t match the capabilities 62 | offered by other developer platforms. You shouldn’t have to be intimately 63 | familiar with web programming to start building great apps for the modern 64 | web. And even though JavaScript engines are getting faster, web apps still 65 | start up much too slowly. 66 | 67 | We expect Dart to help in two main ways: 68 | 69 | 70 | 71 | Better performance: As VM engineers, the 72 | designers of Dart know how to build a language for performance. A more 73 | structured language is easier to optimize, and a fresh VM enables 74 | improvements such as faster startup. 75 | 76 | 77 | 78 | Better productivity: Support for libraries 79 | and packages helps you work with other developers and easily reuse 80 | code from other projects. Types can make APIs clearer and easier to 81 | use. Tools help you refactor, navigate, and debug code. 82 | 83 | 84 |
85 | 86 | 87 | A Quick Look at the Dart Language 88 | 89 | It’s hard to talk about a language without seeing it. Here’s a peek 90 | at a small Dart program: 91 | 92 | lang-dart 93 | import 'dart:math'; 94 | 95 | class Point { 96 | num x, y; 97 | Point(this.x, this.y); 98 | num distanceTo(Point other) { 99 | var dx = x - other.x; 100 | var dy = y - other.y; 101 | return sqrt(dx * dx + dy * dy); 102 | } 103 | } 104 | 105 | main() { 106 | var p = new Point(2, 3); 107 | var q = new Point(3, 4); 108 | print('distance from p to q = ${p.distanceTo(q)}'); 109 | } 110 | 111 | Of course, Dart’s main use case is building modern web apps. 112 | Programming the browser is easy: 113 | 114 | lang-dart 115 | import 'dart:html'; 116 | 117 | main() { 118 | var button = new ButtonElement(); 119 | button..id = 'confirm' 120 | ..text = 'Click to Confirm' 121 | ..classes.add('important') 122 | ..onClick.listen((e) => window.alert('Confirmed!')); 123 | querySelector('#registration').children.add(button); 124 | } 125 | 126 | You’ll learn about the Dart language and libraries in Chapters and respectively. 129 | 130 | 131 | 132 | What’s Cool About Dart 133 | 134 | Dart may look familiar, but don’t let that fool you. Dart has lots 135 | of cool features to help give you a productive and fun experience building 136 | the next generation of awesome web apps. 137 | 138 | Dart is easy to learn. A wide range of 139 | developers can learn Dart quickly. It’s an object-oriented language with 140 | classes, single inheritance, lexical scope, top-level functions, and a 141 | familiar syntax. Most developers are up and running with Dart in just a 142 | few hours. 143 | 144 | Dart compiles to JavaScript. Dart has been 145 | designed from the start to compile to JavaScript, so that Dart apps can 146 | run across the entire modern web. Every feature considered for the 147 | language must somehow be translated to performant and logical JavaScript 148 | before it is added. Dart draws a line in the sand and doesn’t support 149 | older, legacy browsers. 150 | 151 | Dart runs in the client and on the server. The 152 | Dart virtual machine (VM) can be integrated into a web browser, but it can 153 | also run standalone on the command line. With built-in library support for 154 | files, directories, sockets, and even web servers, you can use Dart for 155 | full end-to-end apps. 156 | 157 | Dart comes with a lightweight editor. You can 158 | use Dart Editor to write, launch, and debug Dart apps. The editor can help 159 | you with code completion, detecting potential bugs, debugging both 160 | command-line and web apps, and even refactoring. Dart Editor isn’t 161 | required for writing Dart; it’s just a tool that can help you write better 162 | code faster. 163 | 164 | Dart supports types, without requiring them. 165 | You can omit types when you want to move very quickly, aren’t sure what 166 | structure to take, or simply want to express something you can’t with the 167 | type system. You can add types as your program matures, the structure 168 | becomes more evident, and more developers join the project. Dart’s 169 | optional types are static type annotations that act as documentation, 170 | clearly expressing your intent. Using types means that fewer comments are 171 | required to document the code, and tools can give better warnings and 172 | error messages. 173 | 174 | Dart scales from small scripts to large, complex 175 | apps. Web development is very much an iterative process. With 176 | the reload button acting as your compiler, building the seed of a web app 177 | is often a fun experience of writing a few functions just to experiment. 178 | As the idea grows, you can add more code and structure. Thanks to Dart’s 179 | support for top-level functions, optional types, classes, and libraries, 180 | your Dart programs can start small and grow over time. Tools such as Dart 181 | Editor help you refactor and navigate your code as it evolves. 182 | 183 | Dart has a wide array of built-in libraries. 184 | The core library supports built-in types and other fundamental features 185 | such as collections, dates, and regular expressions. Web apps can use the 186 | HTML library—think DOM programming, but optimized for Dart. Command-line 187 | apps can use the I/O library to work with files, directories, sockets, and 188 | servers. Other libraries include URI, UTF, Crypto, Math, and Unit 189 | test. 190 | 191 | Dart supports safe, simple concurrency with 192 | isolates. Traditional shared-memory threads are difficult to 193 | debug and can lead to deadlocks. Dart’s isolates, inspired by Erlang, 194 | provide an easier to understand model for running isolated, but 195 | concurrent, portions of your code. Spawning new isolates is cheap and 196 | fast, and no state is shared. 197 | 198 | Dart supports code sharing. Traditional web 199 | programming workflows can’t integrate third-party libraries from arbitrary 200 | sources or frameworks. With the Dart package manager (pub) and language 201 | features such as libraries, you can easily discover, install, and 202 | integrate code from across the web and enterprise. 203 | 204 | Dart is open source. Dart was born for the web, 205 | and it’s available under a BSD-style license. You can find the project’s 206 | issue tracker and source 207 | repository online. Maybe you’ll submit the next patch? 208 | 209 | 210 | 211 | Up and Running 212 | 213 | Now that you know something about Dart, get ready to code! These 214 | instructions feature the open-source Dart Editor tool. When you download 215 | Dart, you not only get Dart Editor, but also tools such as the 216 | Dart-to-JavaScript compiler and a version of Chromium (nicknamed 217 | Dartium) that includes the Dart VM. 218 | 219 | 220 | If you run into trouble installing and using Dart Editor, see 221 | Troubleshooting 223 | Dart Editor. 224 | 225 | 226 | 227 | Step 1: Download and Install the Software 228 | 229 | In this step, you’ll install Dart Editor and, if necessary, a Java 230 | runtime environment. (To avoid having to modify the PATH environment 231 | variable, you can install the JRE under your Dart installation 232 | directory, in a subdirectory named jre.) 233 | 234 | 235 | 236 | Download 237 | Dart. 238 | 239 | 240 | 241 | Unarchive the file you downloaded. The resulting directory, 242 | which we’ll call your Dart installation 243 | directory, contains the DartEditor 244 | executable and several subdirectories. 245 | 246 | 247 | 248 | If you don’t already have a Java runtime, download and install 249 | it. Dart Editor requires Java version 6 or higher. 250 | 251 | 252 | 253 | 254 | 255 | Step 2: Launch the Editor 256 | 257 | Go to your Dart installation directory, and double-click the 258 | DartEditor executable 260 | 261 | 263 | 264 | . 265 | 266 | You should see the Dart Editor application window appear, looking 267 | something like . 268 | 269 |
270 | Dart Editor and its Welcome page 271 | 272 | 273 | 274 | 276 | 277 | 278 |
279 |
280 | 281 | 282 | Step 3: Open and Run a Demo 283 | 284 | The Dart Editor bundle comes with several demos and samples. In 285 | this step, you’ll open a web app and run it in Dartium. 286 | 287 | 288 | 289 | Click the Welcome tab. Or 290 | choose Welcome Page from the 291 | Tools menu. 292 | 293 | 294 | 295 | In the Welcome tab, click the image labeled Sunflower. Dart Editor creates a copy of the 297 | Sunflower 299 | app’s directory, and the Editor view displays the contents 300 | of web/sunflower.dart. 301 | 302 | 303 | 304 | Click the Run button 305 | 306 | 307 | 308 | . Dart Editor launches Dartium, which displays 309 | sunflower.html. 311 | 312 | 313 | Dartium is a technical preview, and it might have security 314 | and stability issues. Do not use Dartium as your primary 315 | browser! 316 | 317 | 318 | 319 | 320 | Move the slider to change the sunflower's display, as shown in 321 | . 322 | 323 |
324 | The Sunflower sample running in Dartium 325 | 326 | 327 | 328 | 330 | 331 | 332 |
333 |
334 |
335 |
336 | 337 | 338 | Step 4: Create and Run an App 339 | 340 | It’s easy to create a simple web or command-line app from scratch. 341 | This step walks you through creating and running a command-line 342 | app. 343 | 344 | 345 | 346 | Click the New Application button 347 | 348 | 349 | 350 | (at the upper left of Dart Editor). 351 | Alternatively, choose File > New 352 | Application from the Dart Editor menu, or click the 353 | Create an Application… button in 354 | the Welcome page. A dialog appears (see ). 356 | 357 | 358 | 359 | Type in a name for your application—for example, 360 | hello_world. If you don’t like the default directory, 361 | type in a new location or browse to choose the location. 362 | 363 | 364 | 365 | Make sure Generate sample 366 | content and Command-line 367 | application are selected. Then click Finish to create the initial files for the 369 | app. 370 | 371 |
372 | Create command-line or web apps with Dart Editor 373 | 374 | 375 | 376 | 378 | 379 | 380 |
381 | 382 | A default Dart file appears in the Edit view, and its 383 | directory appears in the Files view. Your Dart Editor window should 384 | look something like . 385 | 386 |
387 | Dart Editor displaying a new app’s files 388 | 389 | 390 | 391 | 393 | 394 | 395 |
396 |
397 | 398 | 399 | Click the Run button 400 | 401 | 402 | 403 | to run your new app. 404 | 405 | For command-line apps, the output of 406 | print() appears at the bottom right, in a new tab 407 | next to the Problems tab. 408 | 409 |
410 |
411 | 412 | 413 | What Next? 414 | 415 | Now that you know the basics, you can learn more about Dart Editor 416 | and help improve it. 417 | 418 | 419 | Follow a code lab 420 | 421 | Go to dartlang.org/codelabs 423 | to find the 424 | latest step-by-step instructions for writing an app. The first code 425 | lab, Try 426 | Dart, guides you through using Dart Editor to build and run a 427 | pirate name badge generator. 428 | 429 | 430 | 431 | Read tutorials 432 | 433 | The Dart 434 | Tutorials teach you how to build web applications using the 435 | Dart language, tools, and APIs. 436 | 437 | 438 | 439 | Become a power user 440 | 441 | Visit the Dart 442 | Editor homepage for help on using Dart Editor’s expanding 443 | feature set. 444 | 445 | 446 | 447 | Send feedback 448 | 449 | Click the SEND FEEDBACK link 450 | (at the upper right of the Dart Editor window) whenever you notice a 451 | problem or have an idea for improving Dart Editor. We’ll open a new 452 | issue for you, if appropriate, without disclosing your sensitive or 453 | personally identifiable information. 454 | 455 | 456 |
457 |
--------------------------------------------------------------------------------