├── .github └── workflows │ └── build.yaml ├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── pubspec.lock ├── pubspec.yaml └── web ├── _reset.scss ├── _site.yaml ├── examples ├── _example.tmpl.html ├── async_await │ ├── async_await.dart │ └── index.md ├── async_star │ ├── async_star.dart │ └── index.md ├── await_for │ ├── await_for.dart │ └── index.md ├── classes │ ├── classes.dart │ └── index.md ├── comments │ ├── comments.dart │ └── index.md ├── const │ ├── const.dart │ └── index.md ├── constructors │ ├── constructors.dart │ └── index.md ├── exceptions │ ├── exceptions.dart │ └── index.md ├── final │ ├── final.dart │ └── index.md ├── for │ ├── for1.dart │ └── index.md ├── functions │ ├── functions.dart │ └── index.md ├── futures │ ├── futures.dart │ └── index.md ├── generators │ ├── generators.dart │ └── index.md ├── getters_setters │ ├── getters_setters.dart │ └── index.md ├── hello_world │ ├── hello_world.dart │ ├── hello_world2.dart │ └── index.md ├── http_request │ ├── http_request.dart │ └── index.md ├── http_server │ ├── http_server.dart │ └── index.md ├── ifelse │ ├── ifelse.dart │ └── index.md ├── inheritance │ ├── index.md │ └── inheritance.dart ├── initializer_lists │ ├── index.md │ └── initializer_lists.dart ├── isolates │ ├── index.md │ └── isolates.dart ├── iterables │ ├── index.md │ └── iterables.dart ├── iterators │ ├── index.md │ └── iterators.dart ├── lexical_scope │ ├── index.md │ ├── lexical_scope.dart │ └── lexical_scope.js ├── libraries │ ├── app.dart │ ├── index.md │ ├── utils.dart │ └── whisper.dart ├── list │ ├── index.md │ └── list.dart ├── map │ ├── index.md │ └── map.dart ├── microtasks │ ├── index.md │ └── microtasks.dart ├── mixins │ ├── index.md │ └── mixins.dart ├── null_aware │ ├── index.md │ └── nullaware1.dart ├── optional_params │ ├── index.md │ └── optional_params.dart ├── pub │ ├── index.md │ └── pubspec.yaml ├── queue │ ├── index.md │ └── queue.dart ├── set │ ├── index.md │ └── set.dart ├── static │ ├── index.md │ └── static.dart ├── streams │ ├── index.md │ └── streams.dart ├── switch │ ├── index.md │ └── switch.dart ├── typedef │ ├── index.md │ └── typedef.dart ├── unused_variables │ ├── index.md │ └── unnamed_variables.dart ├── values │ ├── index.md │ └── values1.dart ├── variables │ ├── index.md │ └── variables1.dart ├── while │ ├── index.md │ └── while.dart ├── yield_star │ ├── index.md │ └── yield_each.dart └── zones │ ├── index.md │ └── zones.dart ├── hljs ├── LICENSE ├── highlight.pack.js └── styles │ └── mono-blue.css ├── index.md ├── site.scss └── templates ├── _example.mustache ├── _example_header.htmlcontent ├── _examples.md ├── _footer.md ├── _header.md ├── _index.mustache └── _intro.md /.github/workflows/build.yaml: -------------------------------------------------------------------------------- 1 | name: Build and deploy 2 | on: 3 | push: 4 | branches: 5 | - main 6 | jobs: 7 | build-and-deploy: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - name: Checkout 11 | uses: actions/checkout@v2.3.1 12 | 13 | - name: Setup Dart 14 | uses: dart-lang/setup-dart@v1.0 15 | 16 | - name: Install and Build 17 | # The symlink can't be resolved by the Deploy action, remove the 18 | # symlink and copy the packages directory. 19 | run: | 20 | dart pub get 21 | dart pub run build_runner build --release --output build/ 22 | rm build/web/packages 23 | mv build/packages build/web/packages 24 | 25 | - name: Deploy 26 | uses: JamesIves/github-pages-deploy-action@4.1.1 27 | with: 28 | branch: gh-pages 29 | folder: build/web 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .pub 2 | packages 3 | .packages 4 | pubspec.lock 5 | .idea/ 6 | build/ 7 | .dart_tool 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 John Ryan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: build 2 | build: 3 | pub run build_runner build --release --output build 4 | rm -r docs 5 | mv build/web docs 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Source code for [Dart By Example](http://jpryan.me/dartbyexample). 2 | 3 | ## Building 4 | 5 | ``` 6 | pub run build_runner build --release --output build/ 7 | ``` 8 | -------------------------------------------------------------------------------- /pubspec.lock: -------------------------------------------------------------------------------- 1 | # Generated by pub 2 | # See https://dart.dev/tools/pub/glossary#lockfile 3 | packages: 4 | _fe_analyzer_shared: 5 | dependency: transitive 6 | description: 7 | name: _fe_analyzer_shared 8 | url: "https://pub.dartlang.org" 9 | source: hosted 10 | version: "14.0.0" 11 | analyzer: 12 | dependency: transitive 13 | description: 14 | name: analyzer 15 | url: "https://pub.dartlang.org" 16 | source: hosted 17 | version: "0.41.2" 18 | args: 19 | dependency: transitive 20 | description: 21 | name: args 22 | url: "https://pub.dartlang.org" 23 | source: hosted 24 | version: "1.6.0" 25 | async: 26 | dependency: transitive 27 | description: 28 | name: async 29 | url: "https://pub.dartlang.org" 30 | source: hosted 31 | version: "2.5.0" 32 | build: 33 | dependency: transitive 34 | description: 35 | name: build 36 | url: "https://pub.dartlang.org" 37 | source: hosted 38 | version: "1.6.2" 39 | build_config: 40 | dependency: transitive 41 | description: 42 | name: build_config 43 | url: "https://pub.dartlang.org" 44 | source: hosted 45 | version: "0.4.5" 46 | build_daemon: 47 | dependency: transitive 48 | description: 49 | name: build_daemon 50 | url: "https://pub.dartlang.org" 51 | source: hosted 52 | version: "2.1.10" 53 | build_resolvers: 54 | dependency: transitive 55 | description: 56 | name: build_resolvers 57 | url: "https://pub.dartlang.org" 58 | source: hosted 59 | version: "1.5.3" 60 | build_runner: 61 | dependency: "direct dev" 62 | description: 63 | name: build_runner 64 | url: "https://pub.dartlang.org" 65 | source: hosted 66 | version: "1.11.1" 67 | build_runner_core: 68 | dependency: transitive 69 | description: 70 | name: build_runner_core 71 | url: "https://pub.dartlang.org" 72 | source: hosted 73 | version: "6.1.7" 74 | built_collection: 75 | dependency: transitive 76 | description: 77 | name: built_collection 78 | url: "https://pub.dartlang.org" 79 | source: hosted 80 | version: "5.0.0" 81 | built_value: 82 | dependency: transitive 83 | description: 84 | name: built_value 85 | url: "https://pub.dartlang.org" 86 | source: hosted 87 | version: "8.0.5" 88 | charcode: 89 | dependency: transitive 90 | description: 91 | name: charcode 92 | url: "https://pub.dartlang.org" 93 | source: hosted 94 | version: "1.2.0" 95 | checked_yaml: 96 | dependency: transitive 97 | description: 98 | name: checked_yaml 99 | url: "https://pub.dartlang.org" 100 | source: hosted 101 | version: "1.0.4" 102 | cli_repl: 103 | dependency: transitive 104 | description: 105 | name: cli_repl 106 | url: "https://pub.dartlang.org" 107 | source: hosted 108 | version: "0.2.2" 109 | cli_util: 110 | dependency: transitive 111 | description: 112 | name: cli_util 113 | url: "https://pub.dartlang.org" 114 | source: hosted 115 | version: "0.3.0" 116 | clock: 117 | dependency: transitive 118 | description: 119 | name: clock 120 | url: "https://pub.dartlang.org" 121 | source: hosted 122 | version: "1.1.0" 123 | code_builder: 124 | dependency: transitive 125 | description: 126 | name: code_builder 127 | url: "https://pub.dartlang.org" 128 | source: hosted 129 | version: "3.7.0" 130 | collection: 131 | dependency: transitive 132 | description: 133 | name: collection 134 | url: "https://pub.dartlang.org" 135 | source: hosted 136 | version: "1.15.0" 137 | convert: 138 | dependency: transitive 139 | description: 140 | name: convert 141 | url: "https://pub.dartlang.org" 142 | source: hosted 143 | version: "3.0.0" 144 | crypto: 145 | dependency: transitive 146 | description: 147 | name: crypto 148 | url: "https://pub.dartlang.org" 149 | source: hosted 150 | version: "3.0.1" 151 | dart_style: 152 | dependency: transitive 153 | description: 154 | name: dart_style 155 | url: "https://pub.dartlang.org" 156 | source: hosted 157 | version: "1.3.12" 158 | file: 159 | dependency: transitive 160 | description: 161 | name: file 162 | url: "https://pub.dartlang.org" 163 | source: hosted 164 | version: "5.2.1" 165 | fixnum: 166 | dependency: transitive 167 | description: 168 | name: fixnum 169 | url: "https://pub.dartlang.org" 170 | source: hosted 171 | version: "1.0.0" 172 | glob: 173 | dependency: transitive 174 | description: 175 | name: glob 176 | url: "https://pub.dartlang.org" 177 | source: hosted 178 | version: "1.2.0" 179 | graphs: 180 | dependency: transitive 181 | description: 182 | name: graphs 183 | url: "https://pub.dartlang.org" 184 | source: hosted 185 | version: "0.2.0" 186 | http: 187 | dependency: "direct main" 188 | description: 189 | name: http 190 | url: "https://pub.dartlang.org" 191 | source: hosted 192 | version: "0.12.2" 193 | http_multi_server: 194 | dependency: transitive 195 | description: 196 | name: http_multi_server 197 | url: "https://pub.dartlang.org" 198 | source: hosted 199 | version: "2.2.0" 200 | http_parser: 201 | dependency: transitive 202 | description: 203 | name: http_parser 204 | url: "https://pub.dartlang.org" 205 | source: hosted 206 | version: "3.1.4" 207 | intl: 208 | dependency: transitive 209 | description: 210 | name: intl 211 | url: "https://pub.dartlang.org" 212 | source: hosted 213 | version: "0.17.0" 214 | io: 215 | dependency: transitive 216 | description: 217 | name: io 218 | url: "https://pub.dartlang.org" 219 | source: hosted 220 | version: "0.3.5" 221 | js: 222 | dependency: transitive 223 | description: 224 | name: js 225 | url: "https://pub.dartlang.org" 226 | source: hosted 227 | version: "0.6.3" 228 | json_annotation: 229 | dependency: transitive 230 | description: 231 | name: json_annotation 232 | url: "https://pub.dartlang.org" 233 | source: hosted 234 | version: "4.0.1" 235 | logging: 236 | dependency: transitive 237 | description: 238 | name: logging 239 | url: "https://pub.dartlang.org" 240 | source: hosted 241 | version: "1.0.1" 242 | markdown: 243 | dependency: "direct main" 244 | description: 245 | name: markdown 246 | url: "https://pub.dartlang.org" 247 | source: hosted 248 | version: "2.1.8" 249 | matcher: 250 | dependency: transitive 251 | description: 252 | name: matcher 253 | url: "https://pub.dartlang.org" 254 | source: hosted 255 | version: "0.12.10" 256 | meta: 257 | dependency: transitive 258 | description: 259 | name: meta 260 | url: "https://pub.dartlang.org" 261 | source: hosted 262 | version: "1.3.0" 263 | mime: 264 | dependency: transitive 265 | description: 266 | name: mime 267 | url: "https://pub.dartlang.org" 268 | source: hosted 269 | version: "1.0.0" 270 | mustache: 271 | dependency: "direct main" 272 | description: 273 | name: mustache 274 | url: "https://pub.dartlang.org" 275 | source: hosted 276 | version: "1.1.1" 277 | node_interop: 278 | dependency: transitive 279 | description: 280 | name: node_interop 281 | url: "https://pub.dartlang.org" 282 | source: hosted 283 | version: "1.2.1" 284 | node_io: 285 | dependency: transitive 286 | description: 287 | name: node_io 288 | url: "https://pub.dartlang.org" 289 | source: hosted 290 | version: "1.2.0" 291 | package_config: 292 | dependency: transitive 293 | description: 294 | name: package_config 295 | url: "https://pub.dartlang.org" 296 | source: hosted 297 | version: "1.9.3" 298 | package_resolver: 299 | dependency: transitive 300 | description: 301 | name: package_resolver 302 | url: "https://pub.dartlang.org" 303 | source: hosted 304 | version: "1.0.10" 305 | path: 306 | dependency: transitive 307 | description: 308 | name: path 309 | url: "https://pub.dartlang.org" 310 | source: hosted 311 | version: "1.8.0" 312 | pedantic: 313 | dependency: transitive 314 | description: 315 | name: pedantic 316 | url: "https://pub.dartlang.org" 317 | source: hosted 318 | version: "1.11.0" 319 | pool: 320 | dependency: transitive 321 | description: 322 | name: pool 323 | url: "https://pub.dartlang.org" 324 | source: hosted 325 | version: "1.5.0" 326 | pub_semver: 327 | dependency: transitive 328 | description: 329 | name: pub_semver 330 | url: "https://pub.dartlang.org" 331 | source: hosted 332 | version: "2.0.0" 333 | pubspec_parse: 334 | dependency: transitive 335 | description: 336 | name: pubspec_parse 337 | url: "https://pub.dartlang.org" 338 | source: hosted 339 | version: "0.1.8" 340 | quiver: 341 | dependency: transitive 342 | description: 343 | name: quiver 344 | url: "https://pub.dartlang.org" 345 | source: hosted 346 | version: "2.1.5" 347 | sass: 348 | dependency: transitive 349 | description: 350 | name: sass 351 | url: "https://pub.dartlang.org" 352 | source: hosted 353 | version: "1.32.8" 354 | sass_builder: 355 | dependency: "direct dev" 356 | description: 357 | name: sass_builder 358 | url: "https://pub.dartlang.org" 359 | source: hosted 360 | version: "2.1.3" 361 | shelf: 362 | dependency: transitive 363 | description: 364 | name: shelf 365 | url: "https://pub.dartlang.org" 366 | source: hosted 367 | version: "0.7.9" 368 | shelf_web_socket: 369 | dependency: transitive 370 | description: 371 | name: shelf_web_socket 372 | url: "https://pub.dartlang.org" 373 | source: hosted 374 | version: "0.2.4+1" 375 | source_maps: 376 | dependency: transitive 377 | description: 378 | name: source_maps 379 | url: "https://pub.dartlang.org" 380 | source: hosted 381 | version: "0.10.10" 382 | source_span: 383 | dependency: transitive 384 | description: 385 | name: source_span 386 | url: "https://pub.dartlang.org" 387 | source: hosted 388 | version: "1.8.1" 389 | stack_trace: 390 | dependency: transitive 391 | description: 392 | name: stack_trace 393 | url: "https://pub.dartlang.org" 394 | source: hosted 395 | version: "1.10.0" 396 | stream_channel: 397 | dependency: transitive 398 | description: 399 | name: stream_channel 400 | url: "https://pub.dartlang.org" 401 | source: hosted 402 | version: "2.1.0" 403 | stream_transform: 404 | dependency: transitive 405 | description: 406 | name: stream_transform 407 | url: "https://pub.dartlang.org" 408 | source: hosted 409 | version: "2.0.0" 410 | string_scanner: 411 | dependency: transitive 412 | description: 413 | name: string_scanner 414 | url: "https://pub.dartlang.org" 415 | source: hosted 416 | version: "1.1.0" 417 | tavern: 418 | dependency: "direct main" 419 | description: 420 | name: tavern 421 | url: "https://pub.dartlang.org" 422 | source: hosted 423 | version: "3.2.1+1" 424 | term_glyph: 425 | dependency: transitive 426 | description: 427 | name: term_glyph 428 | url: "https://pub.dartlang.org" 429 | source: hosted 430 | version: "1.2.0" 431 | timing: 432 | dependency: transitive 433 | description: 434 | name: timing 435 | url: "https://pub.dartlang.org" 436 | source: hosted 437 | version: "0.1.1+3" 438 | tuple: 439 | dependency: transitive 440 | description: 441 | name: tuple 442 | url: "https://pub.dartlang.org" 443 | source: hosted 444 | version: "1.0.3" 445 | typed_data: 446 | dependency: transitive 447 | description: 448 | name: typed_data 449 | url: "https://pub.dartlang.org" 450 | source: hosted 451 | version: "1.3.0" 452 | watcher: 453 | dependency: transitive 454 | description: 455 | name: watcher 456 | url: "https://pub.dartlang.org" 457 | source: hosted 458 | version: "0.9.7+15" 459 | web_socket_channel: 460 | dependency: transitive 461 | description: 462 | name: web_socket_channel 463 | url: "https://pub.dartlang.org" 464 | source: hosted 465 | version: "1.2.0" 466 | yaml: 467 | dependency: transitive 468 | description: 469 | name: yaml 470 | url: "https://pub.dartlang.org" 471 | source: hosted 472 | version: "2.2.1" 473 | sdks: 474 | dart: ">=2.12.0 <3.0.0" 475 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: dartbyexample 2 | publish_to: none 3 | environment: 4 | sdk: '>=2.10.0 <3.0.0' 5 | dependencies: 6 | tavern: ^3.2.1 7 | http: ^0.12.0 8 | mustache: ^1.0.0 9 | markdown: ^2.0.0 10 | dev_dependencies: 11 | build_runner: any 12 | sass_builder: any 13 | -------------------------------------------------------------------------------- /web/_reset.scss: -------------------------------------------------------------------------------- 1 | /* CSS reset: http://meyerweb.com/eric/tools/css/reset/ */ 2 | html, body, div, span, applet, object, iframe, 3 | h1, h2, h3, h4, h5, h6, p, blockquote, pre, 4 | a, abbr, acronym, address, big, cite, code, 5 | del, dfn, em, img, ins, kbd, q, s, samp, 6 | small, strike, strong, sub, sup, tt, var, 7 | b, u, i, center, 8 | dl, dt, dd, ol, ul, li, 9 | fieldset, form, label, legend, 10 | table, caption, tbody, tfoot, thead, tr, th, td, 11 | article, aside, canvas, details, embed, 12 | figure, figcaption, footer, header, hgroup, 13 | menu, nav, output, ruby, section, summary, 14 | time, mark, audio, video { 15 | margin: 0; 16 | padding: 0; 17 | border: 0; 18 | font-size: 100%; 19 | font: inherit; 20 | vertical-align: baseline; 21 | } 22 | article, aside, details, figcaption, figure, 23 | footer, header, hgroup, menu, nav, section { 24 | display: block; 25 | } 26 | body { 27 | line-height: 1; 28 | } 29 | ol, ul { 30 | list-style: none; 31 | } 32 | blockquote, q { 33 | quotes: none; 34 | } 35 | blockquote:before, blockquote:after, 36 | q:before, q:after { 37 | content: ''; 38 | content: none; 39 | } 40 | table { 41 | border-collapse: collapse; 42 | border-spacing: 0; 43 | } 44 | -------------------------------------------------------------------------------- /web/_site.yaml: -------------------------------------------------------------------------------- 1 | title: Dart by Example 2 | uri: http://example.com 3 | author: 4 | name: John Ryan 5 | github: https://github.com/johnpryan 6 | -------------------------------------------------------------------------------- /web/examples/_example.tmpl.html: -------------------------------------------------------------------------------- 1 | 2 |
3 |
7 | {{> web/examples/async_await/async_await.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart async_await.dart
13 | greg gregson
14 | steve steveson
15 | ```
16 |
--------------------------------------------------------------------------------
/web/examples/async_star/async_star.dart:
--------------------------------------------------------------------------------
1 | import 'dart:async';
2 |
3 | main() async {
4 | await for (String msg in printNumbersDownAsync(5)) {
5 | print(msg);
6 | }
7 | }
8 |
9 | Stream
7 | {{> web/examples/async_star/async_star.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart async_star.dart
13 | 5 is odd
14 | 4 is even
15 | 3 is odd
16 | 2 is even
17 | 1 is odd
18 | 0 is even
19 | ```
20 |
--------------------------------------------------------------------------------
/web/examples/await_for/await_for.dart:
--------------------------------------------------------------------------------
1 | import 'dart:async';
2 |
3 | main() async {
4 | await for (int i in numbersDownFrom(5)) {
5 | print('$i bottles of beer');
6 | }
7 | }
8 |
9 | Stream
7 | {{> web/examples/await_for/await_for.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart await_for.dart
13 | 5 bottles of beer
14 | 4 bottles of beer
15 | 3 bottles of beer
16 | 2 bottles of beer
17 | 1 bottles of beer
18 | 0 bottles of beer
19 | ```
20 |
--------------------------------------------------------------------------------
/web/examples/classes/classes.dart:
--------------------------------------------------------------------------------
1 | import 'dart:math';
2 |
3 | class Position {
4 | // properties
5 | int x;
6 | int y;
7 |
8 | // methods
9 | double distanceTo(Position other) {
10 | var dx = other.x - x;
11 | var dy = other.y - y;
12 | return sqrt(dx * dx + dy * dy);
13 | }
14 | }
15 |
16 | main() {
17 | var origin = new Position()
18 | ..x = 0
19 | ..y = 0;
20 |
21 | var p = new Position()
22 | ..x = -5
23 | ..y = 6;
24 |
25 | print(origin.distanceTo(p));
26 | }
27 |
--------------------------------------------------------------------------------
/web/examples/classes/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Classes
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/classes/classes.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart classes.dart
13 | 7.810249675906654
14 | ```
15 |
--------------------------------------------------------------------------------
/web/examples/comments/comments.dart:
--------------------------------------------------------------------------------
1 | /// Represents a two-dimensional position
2 | /// has [x] and [y] properties
3 | ///
4 | /// example code can be defined using a four-space indent:
5 | ///
6 | /// var p = new Position();
7 | ///
8 | class Position {
9 | /*
10 | multi-line comments
11 | */
12 | int x;
13 |
14 | // A regular comment
15 | int y;
16 | }
17 |
18 | main() {
19 | print(new Position().runtimeType);
20 | }
21 |
--------------------------------------------------------------------------------
/web/examples/comments/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Comments
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 | Dartdoc parses comments starting with `///`.
7 | Multiple-line comments and single-line comments
8 | use `/* */` and `//`, respectively
9 |
10 |
11 | {{> web/examples/comments/comments.dart}}
12 |
13 |
14 |
15 | ```bash
16 | $ dart comments.dart
17 | Position
18 | ```
19 |
--------------------------------------------------------------------------------
/web/examples/const/const.dart:
--------------------------------------------------------------------------------
1 | import 'dart:math';
2 |
3 | // compile-time constants are defined using 'const'
4 | const name = "greg";
5 |
6 | // Objects can also be declared at compile-time
7 | const Rectangle
10 | {{> web/examples/const/const.dart}}
11 |
12 |
13 |
14 | ```bash
15 | $ dart const.dart
16 | greg
17 | Rectangle (0, 0) 5 x 5
18 | ```
19 |
--------------------------------------------------------------------------------
/web/examples/constructors/constructors.dart:
--------------------------------------------------------------------------------
1 | import 'dart:math';
2 |
3 | class Position {
4 | int x;
5 | int y;
6 |
7 | // A simple constructor
8 | Position(this.x, this.y);
9 |
10 | // Additional constructors can be defined using named constructors
11 | Position.atOrigin() {
12 | x = 0;
13 | y = 0;
14 | }
15 |
16 | // Factory constructors
17 | factory Position.fromMap(Map
7 | {{> web/examples/constructors/constructors.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart constructors.dart
13 | [30, 40]
14 | [0, 0]
15 | [4, 100]
16 | ```
17 |
--------------------------------------------------------------------------------
/web/examples/exceptions/exceptions.dart:
--------------------------------------------------------------------------------
1 | //
2 | class FoodSpoiledError extends StateError {
3 | FoodSpoiledError(String msg) : super(msg);
4 | }
5 |
6 | class Potato {
7 | int age;
8 |
9 | Potato(this.age);
10 |
11 | String peel() {
12 | if (age > 4) {
13 | throw new FoodSpoiledError('your potato is spoiled');
14 | }
15 | return "peeled";
16 | }
17 | }
18 |
19 | main() {
20 | var p = new Potato(7);
21 |
22 | try {
23 | p.peel();
24 | } on FoodSpoiledError catch(_) {
25 | print("nope nope nope");
26 | }
27 |
28 | // any non-null object can be thrown:
29 | try {
30 | throw("potato");
31 | } catch(_) {
32 | print("caught a flying potato");
33 | }
34 |
35 | // exceptions halt excecution
36 | p.peel();
37 | print('not reached');
38 | }
39 |
--------------------------------------------------------------------------------
/web/examples/exceptions/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Exceptions
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/exceptions/exceptions.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart exceptions.dart
13 | nope nope nope
14 | caught a flying potato
15 | Unhandled exception:
16 | Bad state: your potato is spoiled
17 | #0 Potato.peel (file:///exceptions.dart:13:7)
18 | #1 main (file:///exceptions.dart:36:5)
19 | #2 _startIsolate.
9 | {{> web/examples/final/final.dart}}
10 |
11 |
12 |
13 | ```bash
14 | $ dart final.dart
15 | error
16 | error
17 | ```
18 |
--------------------------------------------------------------------------------
/web/examples/for/for1.dart:
--------------------------------------------------------------------------------
1 | main() {
2 | // A standard for loop
3 | for (var i = 0; i < 3; i++) {
4 | print(i);
5 | }
6 |
7 | // 'for-in' can be used on any class that implements Iterable
8 | var collection = [3, 4, 5];
9 | for (var x in collection) {
10 | print(x);
11 | }
12 |
13 | // Closures will capture the value of the index (Dart is lexically scoped)
14 | var callbacks = [];
15 | for (var i = 6; i < 8; i++) {
16 | callbacks.add(() => print(i));
17 | }
18 | callbacks.forEach((c) => c()); // invoke each callback
19 | }
20 |
--------------------------------------------------------------------------------
/web/examples/for/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: For
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/for/for1.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart for.dart
13 | 0
14 | 1
15 | 2
16 | 3
17 | 4
18 | 5
19 | 6
20 | 7
21 | ```
22 |
--------------------------------------------------------------------------------
/web/examples/functions/functions.dart:
--------------------------------------------------------------------------------
1 | // A simple function definition
2 | yell(str) => str.toUpperCase();
3 |
4 | // Functions can have type annotations
5 | List
7 | {{> web/examples/functions/functions.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart functions.dart
13 | THE WREN
14 | noiselessly.
15 | ```
16 |
--------------------------------------------------------------------------------
/web/examples/futures/futures.dart:
--------------------------------------------------------------------------------
1 | import 'dart:async';
2 |
3 | main() {
4 |
5 | // Passing a callback to then() will invoke
6 | // that callback when the future completes
7 | onReady.then((String status) {
8 | print(status);
9 | });
10 |
11 | // Futures can be chained:
12 | onReady
13 | .then(print)
14 | .then((_) => print('done!'));
15 |
16 | // Futures can throw errors:
17 | onReady.catchError(() {
18 | print('error!');
19 | });
20 | }
21 |
22 | Future
7 | {{> web/examples/futures/futures.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart futures.dart
13 | loaded!
14 | loaded!
15 | done!
16 | ```
17 |
--------------------------------------------------------------------------------
/web/examples/generators/generators.dart:
--------------------------------------------------------------------------------
1 | main() {
2 | evenNumbersDownFrom(7).forEach(print);
3 | }
4 |
5 | // sync* functions return an iterable
6 | Iterable
7 | {{> web/examples/getters_setters/getters_setters.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ getters_setters.dart
13 | x: 10 y: 3
14 | rad: 0.2914567944778671
15 | ```
16 |
--------------------------------------------------------------------------------
/web/examples/hello_world/hello_world.dart:
--------------------------------------------------------------------------------
1 | main() {
2 | print('Hello, World!');
3 | }
4 |
--------------------------------------------------------------------------------
/web/examples/hello_world/hello_world2.dart:
--------------------------------------------------------------------------------
1 | main() => print('Hello, World!');
2 |
--------------------------------------------------------------------------------
/web/examples/hello_world/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Hello World
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 | {{> web/examples/hello_world/hello_world.dart}}
7 |
8 | You can also use arrow function syntax:
9 |
10 | {{> web/examples/hello_world/hello_world2.dart}}
11 |
12 | ```bash
13 | $ dart hello_world.dart
14 | Hello, World!
15 | ```
16 |
17 | [Edit on DartPad](https://dartpad.dartlang.org/?source=c4c2f4d1-ecb7-4f12-9dec-12f1294cdcc0)
18 |
--------------------------------------------------------------------------------
/web/examples/http_request/http_request.dart:
--------------------------------------------------------------------------------
1 | import 'dart:convert';
2 | import 'package:http/http.dart' as http;
3 |
4 | main() async {
5 |
6 | // The http package can be used to make HTTP requests using dart:io
7 | // or from the browser via XMLHttpRequests
8 | var query = 'dartlang';
9 | var requestUrl = 'http://hn.algolia.com/api/v1/search?query=$query';
10 | var response = await http.get(requestUrl);
11 |
12 | // decode the reponse into a Map
13 | var jsonResponse = json.decode(response.body);
14 |
15 | // print a link to the article
16 | var firstResult = jsonResponse['hits'][0];
17 | var firstTitle = firstResult['title'];
18 | var firstLink = firstResult['url'];
19 | print('$firstTitle : $firstLink');
20 | }
21 |
--------------------------------------------------------------------------------
/web/examples/http_request/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: HTTP Requests
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 | {{> web/examples/http_request/http_request.dart}}
7 |
8 | ```bash
9 | $ dart http_request.dart
10 | Dart Lang 1.2 Released : https://www.dartlang.org/
11 | ```
12 |
--------------------------------------------------------------------------------
/web/examples/http_server/http_server.dart:
--------------------------------------------------------------------------------
1 | import 'dart:io';
2 |
3 | main() async {
4 | var server = await HttpServer.bind(InternetAddress.ANY_IP_V4, 8777);
5 | print('serving on port ${server.port}');
6 |
7 | // HttpServer extends Stream
7 | {{> web/examples/http_server/http_server.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart http_server.dart
13 | serving on port 8777
14 | ```
15 |
16 | opening `localhost:8777`:
17 |
18 | ```
19 | welcome to my dart server
20 | ```
21 |
--------------------------------------------------------------------------------
/web/examples/ifelse/ifelse.dart:
--------------------------------------------------------------------------------
1 | main() {
2 |
3 | if (7 % 2 == 0) {
4 | print('7 is even');
5 | } else {
6 | print('7 is odd');
7 | }
8 |
9 | if (8 % 4 == 0) {
10 | print('8 is divisble by 4');
11 | }
12 |
13 | // ternary operators
14 | var isAlive = true;
15 | var monday = isAlive ? 'doctor' : null;
16 | print('monday appointment: $monday');
17 | }
18 |
--------------------------------------------------------------------------------
/web/examples/ifelse/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: If Else
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/ifelse/ifelse.dart}}
8 |
9 |
10 |
11 | ```
12 | $ dart ifelse.dart
13 | 7 is odd
14 | 8 is divisble by 4
15 | monday appointment: doctor
16 | ```
17 |
--------------------------------------------------------------------------------
/web/examples/inheritance/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Inheritance
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/inheritance/inheritance.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart inheritance.dart
13 | Barry has been released: bark!
14 | Pikachu has been released: pika!
15 | ```
16 |
--------------------------------------------------------------------------------
/web/examples/inheritance/inheritance.dart:
--------------------------------------------------------------------------------
1 | /// Abstract classes can't be instantiated,
2 | /// but can contain some implementation.
3 | abstract class Animal {
4 | String name;
5 | Animal(this.name);
6 | String get noise;
7 | }
8 |
9 | /// Classes can be extended to share functionality
10 | class Dog extends Animal {
11 | Dog(String name) : super(name);
12 | String get noise => 'bark!';
13 | }
14 |
15 | /// Classes can implement other classes if they
16 | /// define all of the fields
17 | class Pikachu implements Animal {
18 | String name = 'Pikachu';
19 | String get noise => 'pika!';
20 | }
21 |
22 | void releaseAnimals(Iterable
7 | {{> web/examples/initializer_lists/initializer_lists.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ initializer_lists.dart
13 | x: 2 y: 3
14 | rad: 0.5880026035475675
15 | ```
16 |
--------------------------------------------------------------------------------
/web/examples/initializer_lists/initializer_lists.dart:
--------------------------------------------------------------------------------
1 | import 'dart:math';
2 |
3 | class Position {
4 | final int x;
5 | final int y;
6 | final double rad;
7 |
8 | // An initializer list allows
9 | // fields to be defined before the constructor body.
10 | // This is required for final fields.
11 | Position(int x, int y)
12 | : this.x = x,
13 | this.y = y,
14 | rad = atan2(y, x);
15 | }
16 |
17 | main() {
18 | var p = new Position(2, 3);
19 | print('x: ${p.x} y: ${p.y}');
20 | print('rad: ${p.rad}');
21 | }
22 |
--------------------------------------------------------------------------------
/web/examples/isolates/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Isolates
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/isolates/isolates.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ isolates.dart
13 | received foo
14 | received bar
15 | ```
16 |
--------------------------------------------------------------------------------
/web/examples/isolates/isolates.dart:
--------------------------------------------------------------------------------
1 | import 'dart:async';
2 | import 'dart:isolate';
3 |
4 | main() async {
5 | var receivePort = new ReceivePort();
6 | await Isolate.spawn(echo, receivePort.sendPort);
7 |
8 | // The 'echo' isolate sends it's SendPort as the first message
9 | var sendPort = await receivePort.first;
10 |
11 | var msg = await sendReceive(sendPort, "foo");
12 | print('received $msg');
13 | msg = await sendReceive(sendPort, "bar");
14 | print('received $msg');
15 | }
16 |
17 | // the entry point for the isolate
18 | echo(SendPort sendPort) async {
19 | // Open the ReceivePort for incoming messages.
20 | var port = new ReceivePort();
21 |
22 | // Notify any other isolates what port this isolate listens to.
23 | sendPort.send(port.sendPort);
24 |
25 | await for (var msg in port) {
26 | var data = msg[0];
27 | SendPort replyTo = msg[1];
28 | replyTo.send(data);
29 | if (data == "bar") port.close();
30 | }
31 | }
32 |
33 | /// sends a message on a port, receives the response,
34 | /// and returns the message
35 | Future sendReceive(SendPort port, msg) {
36 | ReceivePort response = new ReceivePort();
37 | port.send([msg, response.sendPort]);
38 | return response.first;
39 | }
--------------------------------------------------------------------------------
/web/examples/iterables/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Iterables
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/iterables/iterables.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart iterables.dart
13 | greg
14 | steve
15 | ```
16 |
--------------------------------------------------------------------------------
/web/examples/iterables/iterables.dart:
--------------------------------------------------------------------------------
1 | main() {
2 | // Iterable is implemented by LinkedList,List, ListQueue, Queue, Runes,
3 | // Set, and more.
4 | var set = new Set()..add('greg')..add('steve');
5 |
6 | for (var name in set) {
7 | print(name);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/web/examples/iterators/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Iterators
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/iterators/iterators.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart iterators.dart
13 | 1
14 | 5
15 | 10
16 | 0
17 | 2
18 | 4
19 | ```
20 |
--------------------------------------------------------------------------------
/web/examples/iterators/iterators.dart:
--------------------------------------------------------------------------------
1 | main() {
2 | var iter = [1,5,10].iterator;
3 | while(iter.moveNext()) {
4 | print(iter.current);
5 | }
6 |
7 | var iterable = new Iterable.generate(3);
8 | var iter2 = iterable.map((n) => n*2).iterator;
9 | while(iter2.moveNext()) {
10 | print(iter2.current);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/web/examples/lexical_scope/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Lexical Scope
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 | Dart is a lexically scoped language.
7 | Loops that declare their variable will have a new version of that variable for each iteration.
8 |
9 |
10 | {{> web/examples/lexical_scope/lexical_scope.dart}}
11 |
12 |
13 |
14 | ```bash
15 | $ dart lexical_scope.dart
16 | 0
17 | 1
18 | 2
19 | ```
20 |
21 | Compared to javascript:
22 |
23 |
24 | {{> web/examples/lexical_scope/lexical_scope.js}}
25 |
26 |
27 |
28 | ```bash
29 | $ node lexical_scope.js
30 | 3
31 | 3
32 | 3
33 | ```
34 |
--------------------------------------------------------------------------------
/web/examples/lexical_scope/lexical_scope.dart:
--------------------------------------------------------------------------------
1 | main() {
2 | var functions = [];
3 |
4 | for (var i = 0; i < 3; i++) {
5 | functions.add(() => i);
6 | }
7 |
8 | functions.forEach((fn) => print(fn()));
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/web/examples/lexical_scope/lexical_scope.js:
--------------------------------------------------------------------------------
1 | var functions = [];
2 |
3 | for (var i = 0; i < 3; i++) {
4 | functions[i] = function() { return i };
5 | }
6 |
7 | functions.forEach(function (fn) { console.log(fn())});
--------------------------------------------------------------------------------
/web/examples/libraries/app.dart:
--------------------------------------------------------------------------------
1 | // app.dart
2 |
3 | library app;
4 |
5 | import 'utils.dart';
6 |
7 | main() {
8 | print(shout('Welcome'));
9 | print(whisper('Welcome'));
10 | }
11 |
--------------------------------------------------------------------------------
/web/examples/libraries/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Libraries
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 | libraries are imported using the `import` keyword:
7 |
8 |
9 | {{> web/examples/libraries/app.dart}}
10 |
11 |
12 |
13 | libraries can be split into *parts* using the `part` and `part of` syntax:
14 |
15 |
16 | {{> web/examples/libraries/utils.dart}}
17 |
18 |
19 |
20 |
21 | {{> web/examples/libraries/whisper.dart}}
22 |
23 |
24 |
25 | ```bash
26 | $ app.dart
27 | WELCOME!!!
28 | welcome...
29 | ```
30 |
--------------------------------------------------------------------------------
/web/examples/libraries/utils.dart:
--------------------------------------------------------------------------------
1 | // utils.dart
2 |
3 | library utils;
4 |
5 | part 'whisper.dart';
6 |
7 | String shout(String inp) => inp.toUpperCase() + '!!!';
8 |
--------------------------------------------------------------------------------
/web/examples/libraries/whisper.dart:
--------------------------------------------------------------------------------
1 | // whisper.dart
2 |
3 | part of utils;
4 |
5 | String whisper(String inp) => inp.toLowerCase() + '...';
6 |
--------------------------------------------------------------------------------
/web/examples/list/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: List
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/list/list.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart list.dart
13 | [a, b, c]
14 | [grow, able]
15 | [also, growable, 42]
16 | error
17 | ```
18 |
--------------------------------------------------------------------------------
/web/examples/list/list.dart:
--------------------------------------------------------------------------------
1 | main() {
2 |
3 | // Specifying the length creates a fixed-length list.
4 | var list = new List(3);
5 | list[0] = 'a';
6 | list[1] = 'b';
7 | list[2] = 'c';
8 | print(list);
9 |
10 | // Leaving out the length creates a growable list.
11 | var growable = new List();
12 | growable.addAll(['grow', 'able']);
13 | print(growable);
14 |
15 | // Lists can be defined using bracket literals.
16 | //
17 | var list2 = ['also', 'growable'];
18 | list2.add(42);
19 | print(list2);
20 |
21 | // modifying growable lists during iteration can cause ConcurrentModificationErrors
22 | var list3 = [47, 3, 25];
23 | try {
24 | for (var item in list3) {
25 | if (item < 10) {
26 | list3.remove(item);
27 | }
28 | }
29 | } catch(e) {
30 | print('error');
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/web/examples/map/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Map
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/map/map.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart map.dart
13 | {blue: false, red: true}
14 | {square: false, triangle: true}
15 | square
16 | triangle
17 | false
18 | true
19 | ```
20 |
--------------------------------------------------------------------------------
/web/examples/map/map.dart:
--------------------------------------------------------------------------------
1 | main() {
2 |
3 | // adding keys
4 | var colors = new Map();
5 | colors['blue'] = false;
6 | colors['red'] = true;
7 | print(colors);
8 |
9 | // curly bracket literals can also be used:
10 | var shapes = {
11 | 'square': false,
12 | 'triangle': true
13 | };
14 | print(shapes);
15 |
16 | // keys and values can be iterated.
17 | // HashMap iterates in arbitrary order, while LinkedHashMap, and SplayTreeMap
18 | // iterate in the order they were inserted into the map.
19 | for (var key in shapes.keys) print(key);
20 | for (var value in shapes.values) print(value);
21 | }
22 |
--------------------------------------------------------------------------------
/web/examples/microtasks/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Microtasks
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/microtasks/microtasks.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart microtasks.dart
13 | hello
14 | there
15 | beautiful
16 | world
17 | ```
18 |
--------------------------------------------------------------------------------
/web/examples/microtasks/microtasks.dart:
--------------------------------------------------------------------------------
1 | import 'dart:async';
2 |
3 | main() {
4 |
5 | // Future() schedules a task on the event queue:
6 | new Future(() => print('world'));
7 | print('hello');
8 |
9 | // scheduleMicrotask() will add the task to the microtask queue:
10 | // Tasks on the microtask queue are executed before the next
11 | // run-loop on the event queue.
12 | scheduleMicrotask(() => print('beautiful'));
13 |
14 | print('there');
15 | }
16 |
--------------------------------------------------------------------------------
/web/examples/mixins/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Mixins
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/mixins/mixins.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart mixins.dart
13 | 7.0710678118654755
14 | 100
15 | ```
16 |
--------------------------------------------------------------------------------
/web/examples/mixins/mixins.dart:
--------------------------------------------------------------------------------
1 | import 'dart:math';
2 |
3 | class Position {
4 | int x;
5 | int y;
6 |
7 | double distanceTo(Position other) {
8 | var dx = other.x - x;
9 | var dy = other.y - y;
10 | return sqrt(dx * dx + dy * dy);
11 | }
12 | }
13 |
14 | class Square {
15 | int width;
16 | int height;
17 |
18 | int get area => width * height;
19 | }
20 |
21 | // Classes can be mixed in using 'with'
22 | class SquareView extends Square with Position {}
23 |
24 | main() {
25 | var origin = new Position()
26 | ..x = 0
27 | ..y = 0;
28 |
29 | var square = new SquareView()
30 | ..x = 5
31 | ..y = 5
32 | ..width = 10
33 | ..height = 10;
34 |
35 | print(square.distanceTo(origin));
36 | print(square.area);
37 | }
38 |
--------------------------------------------------------------------------------
/web/examples/null_aware/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Null-Aware Operators
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/null_aware/nullaware1.dart}}
8 |
9 |
10 |
11 | ```
12 | $ dart nullaware.dart
13 | next appoinment: doctor
14 | next appointment: doctor
15 | length: null
16 | ```
17 |
--------------------------------------------------------------------------------
/web/examples/null_aware/nullaware1.dart:
--------------------------------------------------------------------------------
1 | main() {
2 |
3 | // The ?? operator returns the first expression IFF it is not null
4 | var monday = 'doctor';
5 | var tuesday;
6 | var next = tuesday ?? monday;
7 | print('next appointment: $next');
8 |
9 | // the ??= operator assigns a value IFF it is not null
10 | var wednesday;
11 | next ??= wednesday;
12 | print('next appointment: $next');
13 |
14 | // the ? operator calls a function IFF the object is not null
15 | String thursday;
16 | var length = thursday?.length;
17 | print('length: $length');
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/web/examples/optional_params/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Optional Parameters
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/optional_params/optional_params.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart optional_params.dart
13 | HELLO, WORLD
14 | HELLO, WORLD!!!
15 | hello, world...
16 | ```
17 |
--------------------------------------------------------------------------------
/web/examples/optional_params/optional_params.dart:
--------------------------------------------------------------------------------
1 | // an ordered optional parameter
2 | String yell(String str, [bool exclaim = false]) {
3 | var result = str.toUpperCase();
4 | if (exclaim) result = result + '!!!';
5 | return result;
6 | }
7 |
8 | // named optional parameters
9 | String whisper(String str, {bool mysteriously: false}) {
10 | var result = str.toLowerCase();
11 | if (mysteriously) result = result + '...';
12 | return result;
13 | }
14 |
15 |
16 | main() {
17 | print(yell('Hello, World'));
18 | print(yell('Hello, World', true));
19 | print(whisper('Hello, World', mysteriously: true));
20 | }
21 |
--------------------------------------------------------------------------------
/web/examples/pub/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Pub
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 | Pub is the package manager for Dart.
7 |
8 | A typical package has this file structure:
9 |
10 | ```
11 | cool_package/
12 | bin/
13 | ice
14 | lib/
15 | sodas.dart
16 | src/
17 | pepsi.dart
18 | coke.dart
19 | example/
20 | soda_example.dart
21 | web/
22 | cool_app.dart
23 | index.html
24 | style.css
25 | pubspec.yaml
26 | README.md
27 | ```
28 |
29 | ```yaml
30 | # pubspec.yaml
31 |
32 | name: cool_package # use separate package names with underscores
33 | version: 1.2.3 # use semver
34 | description: >
35 | A really cool package that allows you to
36 | reticulate your splines and transmogrify
37 | your unidirectional dataflow.
38 | author: John Ryan
39 | homepage: johnpryan.github.io
40 | environment:
41 | sdk: ">=0.12.0"
42 | documentation: http://docs.coolpackagedartlang.com
43 | dependencies:
44 | yaml: ^2.1.0
45 | frappe: ^0.4.0
46 | dev_dependencies:
47 | test: ^0.12.0
48 | dependency_overrides:
49 | stream_transformers: 0.2.0
50 | yaml:
51 | path: ../yaml
52 | ```
53 |
54 | `dependency_override`s force all packages
55 | in the dependency graph to use a specific version
56 | (since only one version of a package is allowed.)
57 |
58 | `path:` dependencies are convenient if multiple packages
59 | are being developed on a local machine.
60 |
61 | For details, see [pub package layout conventions](https://www.dartlang.org/tools/pub/package-layout.html).
62 |
--------------------------------------------------------------------------------
/web/examples/pub/pubspec.yaml:
--------------------------------------------------------------------------------
1 | # pubspec.yaml
2 |
3 | name: cool_package # use separate package names with underscores
4 | version: 1.2.3 # use semver
5 | description: >
6 | A really cool package that allows you to
7 | reticulate your splines and transmogrify
8 | your unidirectional dataflow.
9 | author: John Ryan
10 | homepage: johnpryan.github.io
11 | environment:
12 | sdk: ">=0.12.0"
13 | documentation: http://docs.coolpackagedartlang.com
14 | dependencies:
15 | yaml: ^2.1.0
16 | frappe: ^0.4.0
17 | dev_dependencies:
18 | test: ^0.12.0
19 |
20 | # Overridden dependencies force all packages
21 | # in the dependency graph to use a specific version
22 | # (since only one version of a package is allowed)
23 | dependency_overrides:
24 | stream_transformers: 0.2.0
25 |
26 | # If you are developing a package (yaml) locally,
27 | # it's convenient to override using a path depencency
28 | yaml:
29 | path: ../yaml
30 |
31 |
--------------------------------------------------------------------------------
/web/examples/queue/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Queue
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/queue/queue.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart queue.dart
13 | (300, 200)
14 | {100, 500}
15 | ```
16 |
--------------------------------------------------------------------------------
/web/examples/queue/queue.dart:
--------------------------------------------------------------------------------
1 | import 'dart:collection';
2 |
3 | main() {
4 | // Queues are optimized for adding to the head or tail
5 | // Items cannot be accessed by their index.
6 | var q = new Queue.from([300, 200, 100, 500]);
7 |
8 | // Queues implement Iterable:
9 | print(q.takeWhile((i) => i > 100));
10 |
11 | // Consuming a queue
12 | while(q.first > 100) {
13 | q.removeFirst();
14 | }
15 | print(q);
16 | }
17 |
--------------------------------------------------------------------------------
/web/examples/set/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Set
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/set/set.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart set.dart
13 | has gold? true
14 | has platinum? false
15 | {gold, silver, bronze, breakfast, lunch, dinner}
16 | gold
17 | null
18 | {gold, silver, bronze}
19 | ```
20 |
--------------------------------------------------------------------------------
/web/examples/set/set.dart:
--------------------------------------------------------------------------------
1 | main() {
2 | var medals = new Set();
3 | medals.add("gold");
4 | medals.add("silver");
5 | medals.add("bronze");
6 |
7 | // HashSets use the equality operator (==) to determine
8 | // if it already contains an item
9 | medals.add("gold");
10 |
11 | print('has gold? ${medals.contains("gold")}');
12 | print('has platinum? ${medals.contains("platinum")}');
13 |
14 | // Sets can be constructed from Iterables
15 | var meals = new Set.from(['breakfast', 'lunch', 'dinner']);
16 |
17 | // Some built-in features:
18 | print(medals.union(meals));
19 | print(medals.lookup("gold"));
20 | print(medals.lookup("platinum"));
21 | print(medals.difference(meals));
22 | }
23 |
--------------------------------------------------------------------------------
/web/examples/static/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Static
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 | Static members (functions or properties) are available on
7 | the class itself, instead of on an instance of that class.
8 |
9 |
10 | {{> web/examples/static/static.dart}}
11 |
12 |
13 |
14 | ```bash
15 | $ dart static.dart
16 | 100
17 | 200
18 | ```
19 |
--------------------------------------------------------------------------------
/web/examples/static/static.dart:
--------------------------------------------------------------------------------
1 | class Position {
2 | // a static function
3 | static int get maxX => 100;
4 |
5 | // a static property
6 | static int maxY = 200;
7 | }
8 |
9 | main() {
10 | print(Position.maxX);
11 | print(Position.maxY);
12 | }
13 |
--------------------------------------------------------------------------------
/web/examples/streams/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Streams
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/streams/streams.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart streams.dart
13 | s1: 1
14 | s3: 2
15 | s4: 1
16 | s4: 1
17 | s1: 3
18 | s3: 4
19 | s4: 2
20 | s4: 2
21 | s1: 5
22 | s3: 10
23 | s2: tick!
24 | s2: tick!
25 | s2: tick!
26 | ```
27 |
--------------------------------------------------------------------------------
/web/examples/streams/streams.dart:
--------------------------------------------------------------------------------
1 | import 'dart:async';
2 |
3 | main() {
4 | // creates a single subscription stream
5 | new Stream.fromIterable([1, 3, 5]).listen((i) {
6 | print('s1: $i');
7 | });
8 |
9 | // New streams can created from other streams, using
10 | // using methods like where(), map(), expand(), take(), or skip()
11 | new Stream.periodic(new Duration(milliseconds: 300))
12 | .take(3)
13 | .listen((_) => print('s2: tick!'));
14 |
15 | new Stream.fromIterable([1, 2, 5]).map((n) => n * 2).listen((n) => print('s3: $n'));
16 |
17 | // Broadcast streams can be listened to by multiple consumers.
18 | var sc = new StreamController.broadcast();
19 | var broadcastStream = sc.stream;
20 | broadcastStream.listen((v) => print('s4: $v'));
21 | broadcastStream.listen((v) => print('s4: $v'));
22 | sc.add(1);
23 | sc.add(2);
24 | }
25 |
--------------------------------------------------------------------------------
/web/examples/switch/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Switch
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/switch/switch.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart switch.dart
13 | L-shape
14 | diagonal
15 | ```
16 |
--------------------------------------------------------------------------------
/web/examples/switch/switch.dart:
--------------------------------------------------------------------------------
1 | main() {
2 | // a typical switch statement
3 | var piece = 'knight';
4 | switch(piece) {
5 | case 'bishop':
6 | print('diagonal');
7 | break;
8 | case 'knight':
9 | print('L-shape');
10 | break;
11 | default:
12 | print('checkmate');
13 | }
14 |
15 | // cases can only fall through if they are empty:
16 | piece = 'queen';
17 | switch(piece) {
18 | case 'queen':
19 | case 'bishop':
20 | print('diagonal');
21 | break;
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/web/examples/typedef/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Function Types
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/typedef/typedef.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart typedef.dart
13 | true
14 | false
15 | ```
16 |
--------------------------------------------------------------------------------
/web/examples/typedef/typedef.dart:
--------------------------------------------------------------------------------
1 | typedef bool Validator(int n);
2 |
3 | bool positive(int n) => n >= 0;
4 | bool lessThan100(int n) => n < 100;
5 |
6 | bool bothValid(int n, Validator v1, Validator v2) {
7 | return v1(n) && v2(n);
8 | }
9 |
10 | main() {
11 | Validator both = (int n) => bothValid(n, positive, lessThan100);
12 | print('${both(5)}');
13 | print('${both(1000)}');
14 | }
15 |
--------------------------------------------------------------------------------
/web/examples/unused_variables/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Unused Variables
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/unused_variables/unnamed_variables.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dartanalyzer web/examples/unnamed_parameters/unnamed_parameters.dart
13 | [hint] The value of the local variable 'i' is not used (unnamed_parameters.dart, line 2, col 12)
14 | 1 hint found.
15 | ```
16 |
--------------------------------------------------------------------------------
/web/examples/unused_variables/unnamed_variables.dart:
--------------------------------------------------------------------------------
1 | main() {
2 | for (var i in new Iterable.generate(1)) {
3 | print('not using "i"');
4 | }
5 |
6 | // using an underscore silences "local variable is not used"
7 | // warnings when running dartanalyzer
8 | for (var _ in new Iterable.generate(1)) {
9 | print('no warnings');
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/web/examples/values/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Values
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/values/values1.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart values.dart
13 | 1+1=2
14 | 7.0/3.0 =2.3333333333333335
15 | false
16 | true
17 | false
18 | ```
19 |
--------------------------------------------------------------------------------
/web/examples/values/values1.dart:
--------------------------------------------------------------------------------
1 | main() {
2 | // Strings can be appended using +
3 | print("dart" + "lang");
4 |
5 | // Integers and Floats
6 | print("1+1=${1+1}");
7 | print("7.0/3.0 =${7.0/3.0}");
8 |
9 | // Booleans
10 | print(true && false);
11 | print(false || true);
12 | print(!true);
13 | }
14 |
--------------------------------------------------------------------------------
/web/examples/variables/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Variables
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/variables/variables1.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart variables.dart
13 | initial
14 | 42
15 | 99
16 | 44.0
17 | ```
--------------------------------------------------------------------------------
/web/examples/variables/variables1.dart:
--------------------------------------------------------------------------------
1 | main() {
2 | // 'var' declares a variable. dartanalyzer infers the type.
3 | var a = "initial";
4 | print(a);
5 |
6 | // The type can also be declared:
7 | num b = 42;
8 | print(b);
9 |
10 | // final variables cannot be changed once declared
11 | final num c = 99;
12 | print(c);
13 |
14 | // const variables are compile-time constants
15 | const double d = 44.00;
16 | print(d);
17 | }
18 |
--------------------------------------------------------------------------------
/web/examples/while/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: While
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/while/while.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart while.dart
13 | while 1
14 | while 2
15 | dowhile 0
16 | dowhile 1
17 | dowhile 2
18 | ```
19 |
--------------------------------------------------------------------------------
/web/examples/while/while.dart:
--------------------------------------------------------------------------------
1 | main() {
2 | var i = 0;
3 |
4 | // while conditions are evaluated before the loop
5 | while(i++ < 2) {
6 | print("while $i");
7 | }
8 |
9 | var j = 0;
10 |
11 | // do-while conditions are evaluated after the loop
12 | do {
13 | print("dowhile $j");
14 | } while (j++ < 2);
15 | }
16 |
--------------------------------------------------------------------------------
/web/examples/yield_star/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Yield*
3 | template: web/templates/_example.mustache
4 | ---
5 |
6 |
7 | {{> web/examples/yield_star/yield_each.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart yield_each.dart
13 | 5 bottles of beer
14 | 4 bottles of beer
15 | 3 bottles of beer
16 | 2 bottles of beer
17 | 1 bottles of beer
18 | 0 bottles of beer
19 | ```
20 |
--------------------------------------------------------------------------------
/web/examples/yield_star/yield_each.dart:
--------------------------------------------------------------------------------
1 | import 'dart:async';
2 |
3 | main() async {
4 | await for (int i in numbersDownFrom(5)) {
5 | print('$i bottles of beer');
6 | }
7 | }
8 |
9 | Stream
7 | {{> web/examples/zomes/zones.dart}}
8 |
9 |
10 |
11 | ```bash
12 | $ dart zones.dart
13 | 2015-10-16 15:51:25.893: received request: foo
14 | 2015-10-16 15:51:25.998: received request: bar
15 | caught: unrecognized request: bar
16 | ```
17 |
--------------------------------------------------------------------------------
/web/examples/zones/zones.dart:
--------------------------------------------------------------------------------
1 | import 'dart:async';
2 |
3 | main() {
4 | // All Dart programs implicitly run in a root zone.
5 | // runZoned creates a new zone. The new zone is a child of the root zone.
6 | runZoned(() async {
7 | await runServer();
8 | },
9 | // Any uncaught errors in the child zone are sent to the [onError] handler.
10 | onError: (e, stacktrace) {
11 | print('caught: $e');
12 | },
13 | // a ZoneSpecification allows for overriding functionality, like print()
14 | zoneSpecification: new ZoneSpecification(print: (Zone self, ZoneDelegate parent, Zone zone, String message) {
15 | parent.print(zone, '${new DateTime.now()}: $message');
16 | })
17 | );
18 | }
19 |
20 | Future runServer() async {
21 | await for (var r in requests()) {
22 | print('received request: $r');
23 | if (r == 'bar') throw('unrecognized request: $r');
24 | }
25 | }
26 |
27 | Stream