├── .gitignore ├── AUTHORS ├── LICENSE ├── README.md ├── anagram ├── .gitignore ├── analysis_options.yaml ├── pubspec.yaml └── web │ ├── favicon.ico │ ├── index.html │ ├── main.dart │ └── styles.css ├── analysis_options.yaml ├── cmdline ├── .gitignore ├── analysis_options.yaml ├── bin │ ├── dgrep.dart │ └── helloworld42.dart └── pubspec.yaml ├── dart_test.yaml ├── futures ├── async-await-catch-error │ ├── .gitignore │ ├── analysis_options.yaml │ ├── pubspec.yaml │ └── web │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── main.dart │ │ └── styles.css ├── async-await │ ├── .gitignore │ ├── analysis_options.yaml │ ├── pubspec.yaml │ └── web │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── main.dart │ │ └── styles.css ├── futures-api-catch-error │ ├── .gitignore │ ├── analysis_options.yaml │ ├── pubspec.yaml │ └── web │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── main.dart │ │ └── styles.css ├── futures-api │ ├── .gitignore │ ├── analysis_options.yaml │ ├── pubspec.yaml │ └── web │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── main.dart │ │ └── styles.css ├── sequential-processing │ ├── .gitignore │ ├── analysis_options.yaml │ ├── pubspec.yaml │ └── web │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── main.dart │ │ └── styles.css └── waiting-on-futures │ ├── .gitignore │ ├── analysis_options.yaml │ ├── pubspec.yaml │ └── web │ ├── favicon.ico │ ├── index.html │ ├── main.dart │ └── styles.css ├── httpserver ├── .gitignore ├── analysis_options.yaml ├── bin │ ├── basic_file_server.dart │ ├── basic_writer_client.dart │ ├── basic_writer_server.dart │ ├── hello_world_server.dart │ ├── hello_world_server_secure.dart │ ├── index.html │ ├── mini_file_server.dart │ ├── note_server.dart │ ├── notes.txt │ ├── number_guesser.dart │ ├── number_thinker.dart │ ├── server_authority.pem │ ├── server_chain.pem │ ├── server_key.pem │ └── static_file_server.dart ├── pubspec.yaml ├── test │ └── httpserver_test.dart └── web │ ├── make_a_guess.html │ ├── note_client.dart │ └── note_taker.html ├── its_all_about_you ├── .gitignore ├── analysis_options.yaml ├── pubspec.yaml └── web │ ├── index.html │ ├── main.dart │ └── styles.css ├── mini ├── .gitignore ├── analysis_options.yaml ├── pubspec.yaml └── web │ ├── index.html │ └── main.dart ├── mini_with_style ├── .gitignore ├── analysis_options.yaml ├── pubspec.yaml └── web │ ├── index.html │ ├── main.dart │ └── styles.css ├── portmanteaux ├── .gitignore ├── analysis_options.yaml ├── pubspec.yaml └── web │ ├── index.html │ ├── main.dart │ └── styles.css ├── portmanteaux_simple ├── .gitignore ├── analysis_options.yaml ├── pubspec.yaml └── web │ ├── index.html │ ├── main.dart │ └── styles.css ├── runtests.sh ├── search_form ├── .gitignore ├── analysis_options.yaml ├── pubspec.yaml └── web │ ├── index.html │ ├── readme │ └── styles.css ├── streams ├── last_positive │ ├── .gitignore │ ├── analysis_options.yaml │ ├── bin │ │ └── main.dart │ └── pubspec.yaml ├── read_file │ ├── .gitignore │ ├── analysis_options.yaml │ ├── bin │ │ └── main.dart │ └── pubspec.yaml ├── simple_stream │ ├── .gitignore │ ├── analysis_options.yaml │ ├── bin │ │ └── main.dart │ └── pubspec.yaml └── throw_error │ ├── .gitignore │ ├── analysis_options.yaml │ ├── bin │ └── main.dart │ └── pubspec.yaml ├── todo ├── .gitignore ├── analysis_options.yaml ├── pubspec.yaml └── web │ ├── index.html │ ├── main.dart │ └── styles.css └── todo_with_delete ├── .gitignore ├── analysis_options.yaml ├── pubspec.yaml └── web ├── index.html ├── main.dart └── styles.css /.gitignore: -------------------------------------------------------------------------------- 1 | .dart_tool/ 2 | .packages 3 | pubspec.lock 4 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | # Below is a list of people and organizations that have contributed 2 | # to the Dart project. Names should be added to the list like so: 3 | # 4 | # Name/Organization 5 | # 6 | # If you contributed to the project(s) in this repository, and would 7 | # like your name included here, please contact misc@dartlang.org mailing list. 8 | 9 | Google Inc. 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012, the Dart project authors. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following 11 | disclaimer in the documentation and/or other materials provided 12 | with the distribution. 13 | * Neither the name of Google LLC nor the names of its 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## DISCONTINUED 2 | 3 | This repo has been discontinued. 4 | 5 | For current samples, please see: 6 | 7 | * https://dart.dev/samples 8 | * https://github.com/dart-lang/samples/ 9 | 10 | --- 11 | 12 | These are small Dart samples used by the online 13 | [Dart tutorials](https://dart.dev/tutorials). 14 | 15 | Each directory in this repo represents a tutorial. 16 | The `homepage` field in each pubspec file points to the 17 | corresponding tutorial on [dart.dev](https://dart.dev/). 18 | 19 | ## DartPad and Gist Files 20 | 21 | Some of the tutorial examples execute using 22 | [DartPad](https://dartpad.dev/). 23 | The DartPad examples are organized so that each complete example is 24 | placed in a subdirectory, with its own pubspec and **web/** directory. 25 | 26 | DartPad grabs the source for these examples from 27 | [gist](https://gist.github.com/) files. 28 | The gist files are generated from the source in this repo using the 29 | [gist generator](https://github.com/kasperpeulen/gist-generator). 30 | 31 | You can update the gist files, as follows: 32 | 33 | * Clone the repo. 34 | * If needed, activate the gist executable: 35 | `pub global activate --source git https://github.com/kasperpeulen/gist-generator` 36 | * Change directory to the top of the repo. 37 | * Run "gist generate". 38 | See the [readme](https://github.com/kasperpeulen/gist-generator) 39 | for information on available options. If you are updating, or creating, 40 | gist files rather than generating a test gist, 41 | or performing a dry run of the gist generator, 42 | you'll need to provide a Gist token. 43 | (See the project lead for info on obtaining an existing Gist token.) 44 | 45 | ## Additional files 46 | 47 | **README.md:** 48 | This file. 49 | 50 | **runtests.sh:** 51 | BASH script that runs dartanalyzer on all Dart source files in the web/ and bin/ directories. 52 | 53 | -------------------------------------------------------------------------------- /anagram/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /anagram/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /anagram/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: anagram 2 | description: A sample application 3 | homepage: https://dart.dev/tutorials/web/low-level-html/add-elements 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /anagram/web/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-tutorials-samples/111938d000c5204e31e80be9cf4128739146aa0d/anagram/web/favicon.ico -------------------------------------------------------------------------------- /anagram/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Anagram 17 | 18 | 19 | 20 | 21 | 22 | 23 |

Anagram

24 | 25 |

Pile:

26 |
27 |
28 | 29 |

Word:

30 |
31 |
32 | 33 |

Scrabble Value:

34 |

35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /anagram/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012, the Dart project authors. Please see the 2 | // AUTHORS file for details. All rights reserved. Use of this 3 | // source code is governed by a BSD-style license that can be 4 | // found in the LICENSE file. 5 | 6 | import 'dart:html'; 7 | import 'dart:math'; 8 | 9 | // Should remove tiles from here when they are selected otherwise 10 | // the ratio is off. 11 | 12 | String scrabbleLetters = 13 | 'aaaaaaaaabbccddddeeeeeeeeeeeeffggghhiiiiiiiiijkllllmmnnnnnnooooooooppqrrrrrrssssttttttuuuuvvwwxyyz**'; 14 | 15 | List buttons = List(); 16 | Element letterpile; 17 | Element result; 18 | ButtonElement clearButton; 19 | Element value; 20 | int wordvalue = 0; 21 | 22 | Map scrabbleValues = { 23 | 'a': 1, 24 | 'e': 1, 25 | 'i': 1, 26 | 'l': 1, 27 | 'n': 1, 28 | 'o': 1, 29 | 'r': 1, 30 | 's': 1, 31 | 't': 1, 32 | 'u': 1, 33 | 'd': 2, 34 | 'g': 2, 35 | 'b': 3, 36 | 'c': 3, 37 | 'm': 3, 38 | 'p': 3, 39 | 'f': 4, 40 | 'h': 4, 41 | 'v': 4, 42 | 'w': 4, 43 | 'y': 4, 44 | 'k': 5, 45 | 'j': 8, 46 | 'x': 8, 47 | 'q': 10, 48 | 'z': 10, 49 | '*': 0 50 | }; 51 | 52 | void main() { 53 | letterpile = querySelector("#letterpile"); 54 | result = querySelector("#result"); 55 | value = querySelector("#value"); 56 | 57 | clearButton = querySelector("#clearButton"); 58 | clearButton.onClick.listen(newletters); 59 | 60 | generateNewLetters(); 61 | } 62 | 63 | void moveLetter(Event e) { 64 | Element letter = e.target; 65 | if (letter.parent == letterpile) { 66 | result.children.add(letter); 67 | wordvalue += scrabbleValues[letter.text]; 68 | value.text = "$wordvalue"; 69 | } else { 70 | letterpile.children.add(letter); 71 | wordvalue -= scrabbleValues[letter.text]; 72 | value.text = "$wordvalue"; 73 | } 74 | } 75 | 76 | void newletters(Event e) { 77 | letterpile.children.clear(); 78 | result.children.clear(); 79 | generateNewLetters(); 80 | } 81 | 82 | void generateNewLetters() { 83 | final indexGenerator = Random(); 84 | wordvalue = 0; 85 | value.text = ''; 86 | buttons.clear(); 87 | for (var i = 0; i < 7; i++) { 88 | final letterIndex = indexGenerator.nextInt(scrabbleLetters.length); 89 | // Should remove the letter from scrabbleLetters to keep the 90 | // ratio correct. 91 | buttons.add(ButtonElement()); 92 | buttons[i].classes.add("letter"); 93 | buttons[i].onClick.listen(moveLetter); 94 | buttons[i].text = scrabbleLetters[letterIndex]; 95 | letterpile.children.add(buttons[i]); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /anagram/web/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #F8F8F8; 3 | font-family: 'Open Sans', sans-serif; 4 | font-size: 14px; 5 | font-weight: normal; 6 | line-height: 1.2em; 7 | margin: 15px; 8 | } 9 | 10 | .letter { 11 | width: 48px; 12 | height: 48px; 13 | font-size: 32px; 14 | background-color: Lavender; 15 | color: purple; 16 | border: 1px solid black; 17 | margin: 2px 2px 2px 2px; 18 | } 19 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Supported lint rules and documentation: 2 | # http://dart-lang.github.io/linter/lints/ 3 | 4 | analyzer: 5 | exclude: 6 | - bin/helloworld42.dart 7 | 8 | linter: 9 | rules: 10 | - avoid_empty_else 11 | - cancel_subscriptions 12 | - close_sinks 13 | - comment_references 14 | - control_flow_in_finally 15 | - empty_statements 16 | - hash_and_equals 17 | - iterable_contains_unrelated_type 18 | - list_remove_unrelated_type 19 | - test_types_in_equals 20 | - throw_in_finally 21 | - unrelated_type_equality_checks 22 | - valid_regexps 23 | 24 | # - always_declare_return_types -- doesn't interact well with strong mode 25 | # - always_specify_types -- doesn't interact well with strong mode 26 | - annotate_overrides 27 | # - avoid_as -- jmesserly recommends avoiding 28 | - avoid_init_to_null 29 | - avoid_return_types_on_setters 30 | - await_only_futures 31 | - camel_case_types 32 | - constant_identifier_names 33 | - empty_catches 34 | - empty_constructor_bodies 35 | - implementation_imports 36 | - library_names 37 | - library_prefixes 38 | - non_constant_identifier_names 39 | - one_member_abstracts 40 | - only_throw_errors 41 | - overridden_fields 42 | # - package_api_docs # This is example code, not public APIs 43 | - package_prefixed_library_names 44 | - prefer_is_not_empty 45 | # - public_member_api_docs # This is example code, not public APIs 46 | - slash_for_doc_comments 47 | - sort_constructors_first 48 | - sort_unnamed_constructors_first 49 | # - type_annotate_public_apis 50 | - type_init_formals 51 | - unnecessary_brace_in_string_interps 52 | - unnecessary_getters_setters 53 | 54 | - package_names 55 | -------------------------------------------------------------------------------- /cmdline/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /cmdline/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /cmdline/bin/dgrep.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | library dgrep; 6 | 7 | import 'dart:io'; 8 | import 'package:args/args.dart'; 9 | 10 | const usage = 'usage: dart dgrep.dart [-rnS] patterns file_or_directory'; 11 | const recursive = 'recursive'; 12 | const lineNumber = 'line-number'; 13 | const followLinks = 'follow-links'; 14 | 15 | ArgResults argResults; 16 | 17 | void printMatch(File file, List lines, int i) { 18 | final sb = StringBuffer(); 19 | if (argResults[recursive]) sb.write('${file.path}:'); 20 | if (argResults[lineNumber]) sb.write('${i + 1}:'); 21 | sb.write(lines[i]); 22 | print(sb.toString()); 23 | } 24 | 25 | Future searchFile(File file, List searchTerms) async { 26 | try { 27 | var lines = await file.readAsLines(); 28 | for (var i = 0; i < lines.length; i++) { 29 | var found = false; 30 | for (var j = 0; j < searchTerms.length && !found; j++) { 31 | if (lines[i].contains(searchTerms[j])) { 32 | printMatch(file, lines, i); 33 | found = true; 34 | } 35 | } 36 | } 37 | } catch (e) { 38 | print(e); 39 | } 40 | } 41 | 42 | Future main(List arguments) async { 43 | final parser = ArgParser() 44 | ..addFlag(recursive, negatable: false, abbr: 'r') 45 | ..addFlag(lineNumber, negatable: false, abbr: 'n') 46 | ..addFlag(followLinks, negatable: false, abbr: 'S'); 47 | 48 | argResults = parser.parse(arguments); 49 | 50 | if (argResults.rest.length < 2) { 51 | print(usage); 52 | exit(1); 53 | } 54 | 55 | final searchPath = argResults.rest.last; 56 | final searchTerms = argResults.rest.sublist(0, argResults.rest.length - 1); 57 | 58 | if (await FileSystemEntity.isDirectory(searchPath)) { 59 | final startingDir = Directory(searchPath); 60 | await for (var entity in startingDir.list( 61 | recursive: argResults[recursive], 62 | followLinks: argResults[followLinks])) { 63 | if (entity is File) { 64 | await searchFile(entity, searchTerms); 65 | } else { 66 | await searchFile(File(searchPath), searchTerms); 67 | } 68 | } 69 | } else { 70 | await searchFile(File(searchPath), searchTerms); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /cmdline/bin/helloworld42.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | // int fortyTwo = 'Hello, World'; // Type mismatch 3 | // print(fortyTwo); 4 | } 5 | -------------------------------------------------------------------------------- /cmdline/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: cmdline 2 | description: A sample application 3 | homepage: https://dart.dev/tutorials/server/cmdline 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dependencies: 9 | args: any 10 | dart_style: any 11 | test: any 12 | 13 | dev_dependencies: 14 | pedantic: ^1.7.0 15 | 16 | executables: 17 | dcat: 18 | dgrep: 19 | -------------------------------------------------------------------------------- /dart_test.yaml: -------------------------------------------------------------------------------- 1 | # Config docs: https://github.com/dart-lang/test/blob/master/doc/configuration.md 2 | 3 | # Headless chrome info, see 4 | # - https://developers.google.com/web/updates/2017/04/headless-chrome 5 | # - https://developers.google.com/web/updates/2017/06/headless-karma-mocha-chai 6 | 7 | define_platforms: 8 | chromeheadless: 9 | name: ChromeHeadless 10 | extends: chrome 11 | settings: 12 | arguments: --headless --disable-gpu --remote-debugging-port=9222 13 | -------------------------------------------------------------------------------- /futures/async-await-catch-error/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /futures/async-await-catch-error/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /futures/async-await-catch-error/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: async_await_catch_error 2 | description: A sample application that reads a file on dartlang using async/await, and prints the contents; errors are caught. 3 | homepage: https://dart.dev/tutorials/language/futures 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /futures/async-await-catch-error/web/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-tutorials-samples/111938d000c5204e31e80be9cf4128739146aa0d/futures/async-await-catch-error/web/favicon.ico -------------------------------------------------------------------------------- /futures/async-await-catch-error/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | async_await_catch_error 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /futures/async-await-catch-error/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'dart:html'; 6 | import 'dart:async'; 7 | 8 | final output = querySelector('#output'); 9 | final pre = PreElement(); 10 | 11 | Future printDailyNewsDigest() async { 12 | try { 13 | final news = await gatherNewsReports(); 14 | pre.appendText(news); 15 | } catch (e) { 16 | pre.appendText('Something went wrong: $e'); 17 | } 18 | } 19 | 20 | void main() { 21 | output.append(pre); 22 | 23 | printDailyNewsDigest(); 24 | printWinningLotteryNumbers(); 25 | printWeatherForecast(); 26 | printBaseballScore(); 27 | } 28 | 29 | void printWinningLotteryNumbers() { 30 | pre.appendText('Winning lotto numbers: [23, 63, 87, 26, 2]\n'); 31 | } 32 | 33 | void printWeatherForecast() { 34 | pre.appendText('Tomorrow\'s forecast: 70F, sunny.\n'); 35 | } 36 | 37 | void printBaseballScore() { 38 | pre.appendText('Baseball score: Red Sox 10, Yankees 0\n'); 39 | } 40 | 41 | // Imagine that this function is more complex and slow. :) 42 | Future gatherNewsReports() async { 43 | final path = 'https://dart.dev/f/dailyNewsDigest.txt'; 44 | final content = await HttpRequest.getString(path); 45 | return content; 46 | } 47 | -------------------------------------------------------------------------------- /futures/async-await-catch-error/web/styles.css: -------------------------------------------------------------------------------- 1 | @import url(https://fonts.googleapis.com/css?family=Roboto); 2 | 3 | html, body { 4 | width: 100%; 5 | height: 100%; 6 | margin: 0; 7 | padding: 0; 8 | font-family: 'Roboto', sans-serif; 9 | } 10 | 11 | #output { 12 | padding: 20px; 13 | } 14 | -------------------------------------------------------------------------------- /futures/async-await/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /futures/async-await/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /futures/async-await/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: async_await 2 | description: A sample application that reads a file on dartlang using async/await, and prints the contents. 3 | homepage: https://dart.dev/tutorials/language/futures 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /futures/async-await/web/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-tutorials-samples/111938d000c5204e31e80be9cf4128739146aa0d/futures/async-await/web/favicon.ico -------------------------------------------------------------------------------- /futures/async-await/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | async_await 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /futures/async-await/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'dart:html'; 6 | import 'dart:async'; 7 | 8 | final output = querySelector('#output'); 9 | final pre = PreElement(); 10 | 11 | Future printDailyNewsDigest() async { 12 | final news = await gatherNewsReports(); 13 | pre.appendText(news); 14 | } 15 | 16 | void main() { 17 | output.append(pre); 18 | 19 | printDailyNewsDigest(); 20 | printWinningLotteryNumbers(); 21 | printWeatherForecast(); 22 | printBaseballScore(); 23 | } 24 | 25 | void printWinningLotteryNumbers() { 26 | pre.appendText('Winning lotto numbers: [23, 63, 87, 26, 2]\n'); 27 | } 28 | 29 | void printWeatherForecast() { 30 | pre.appendText('Tomorrow\'s forecast: 70F, sunny.\n'); 31 | } 32 | 33 | void printBaseballScore() { 34 | pre.appendText('Baseball score: Red Sox 10, Yankees 0\n'); 35 | } 36 | 37 | // Imagine that this function is more complex and slow. :) 38 | Future gatherNewsReports() async { 39 | final path = 'https://dart.dev/f/dailyNewsDigest.txt'; 40 | return (await HttpRequest.getString(path)); 41 | } 42 | -------------------------------------------------------------------------------- /futures/async-await/web/styles.css: -------------------------------------------------------------------------------- 1 | @import url(https://fonts.googleapis.com/css?family=Roboto); 2 | 3 | html, body { 4 | width: 100%; 5 | height: 100%; 6 | margin: 0; 7 | padding: 0; 8 | font-family: 'Roboto', sans-serif; 9 | } 10 | 11 | #output { 12 | padding: 20px; 13 | } 14 | -------------------------------------------------------------------------------- /futures/futures-api-catch-error/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /futures/futures-api-catch-error/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /futures/futures-api-catch-error/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: futures_api_catch_error 2 | description: A sample application that reads a file on dartlang using the Futures API, and prints the contents; errors are caught. 3 | homepage: https://dart.dev/tutorials/language/futures 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /futures/futures-api-catch-error/web/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-tutorials-samples/111938d000c5204e31e80be9cf4128739146aa0d/futures/futures-api-catch-error/web/favicon.ico -------------------------------------------------------------------------------- /futures/futures-api-catch-error/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | futures_api_catch_error 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /futures/futures-api-catch-error/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'dart:html'; 6 | import 'dart:async'; 7 | 8 | final output = querySelector('#output'); 9 | final pre = PreElement(); 10 | 11 | void printDailyNewsDigest() { 12 | final future = gatherNewsReports(); 13 | future 14 | .then((content) => pre.appendText(content)) 15 | .catchError((e) => handleError(e)); 16 | } 17 | 18 | void main() { 19 | output.append(pre); 20 | 21 | printDailyNewsDigest(); 22 | printWinningLotteryNumbers(); 23 | printWeatherForecast(); 24 | printBaseballScore(); 25 | } 26 | 27 | void handleError(e) { 28 | pre.appendText('handleError: $e'); 29 | } 30 | 31 | void printWinningLotteryNumbers() { 32 | pre.appendText('Winning lotto numbers: [23, 63, 87, 26, 2]\n'); 33 | } 34 | 35 | void printWeatherForecast() { 36 | pre.appendText('Tomorrow\'s forecast: 70F, sunny.\n'); 37 | } 38 | 39 | void printBaseballScore() { 40 | pre.appendText('Baseball score: Red Sox 10, Yankees 0\n'); 41 | } 42 | 43 | // Imagine that this function is more complex and slow. :) 44 | Future gatherNewsReports() { 45 | final path = 'https://dart.dev/f/dailyNewsDigest.txt'; 46 | return HttpRequest.getString(path); 47 | } 48 | -------------------------------------------------------------------------------- /futures/futures-api-catch-error/web/styles.css: -------------------------------------------------------------------------------- 1 | @import url(https://fonts.googleapis.com/css?family=Roboto); 2 | 3 | html, body { 4 | width: 100%; 5 | height: 100%; 6 | margin: 0; 7 | padding: 0; 8 | font-family: 'Roboto', sans-serif; 9 | } 10 | 11 | #output { 12 | padding: 20px; 13 | } 14 | -------------------------------------------------------------------------------- /futures/futures-api/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /futures/futures-api/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /futures/futures-api/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: futures_api 2 | description: A sample application that reads a file on dartlang using the Futures API, and prints the contents. 3 | homepage: https://dart.dev/tutorials/language/futures 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /futures/futures-api/web/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-tutorials-samples/111938d000c5204e31e80be9cf4128739146aa0d/futures/futures-api/web/favicon.ico -------------------------------------------------------------------------------- /futures/futures-api/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | futures_api 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /futures/futures-api/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'dart:html'; 6 | import 'dart:async'; 7 | 8 | final output = querySelector('#output'); 9 | final pre = PreElement(); 10 | 11 | void printDailyNewsDigest() { 12 | final future = gatherNewsReports(); 13 | future.then((content) => pre.appendText(content)); 14 | } 15 | 16 | void main() { 17 | output.append(pre); 18 | 19 | printDailyNewsDigest(); 20 | printWinningLotteryNumbers(); 21 | printWeatherForecast(); 22 | printBaseballScore(); 23 | } 24 | 25 | void printWinningLotteryNumbers() { 26 | pre.appendText('Winning lotto numbers: [23, 63, 87, 26, 2]\n'); 27 | } 28 | 29 | void printWeatherForecast() { 30 | pre.appendText('Tomorrow\'s forecast: 70F, sunny.\n'); 31 | } 32 | 33 | void printBaseballScore() { 34 | pre.appendText('Baseball score: Red Sox 10, Yankees 0\n'); 35 | } 36 | 37 | // Imagine that this function is more complex and slow. :) 38 | Future gatherNewsReports() { 39 | final path = 'https://dart.dev/f/dailyNewsDigest.txt'; 40 | return HttpRequest.getString(path); 41 | } 42 | -------------------------------------------------------------------------------- /futures/futures-api/web/styles.css: -------------------------------------------------------------------------------- 1 | @import url(https://fonts.googleapis.com/css?family=Roboto); 2 | 3 | html, body { 4 | width: 100%; 5 | height: 100%; 6 | margin: 0; 7 | padding: 0; 8 | font-family: 'Roboto', sans-serif; 9 | } 10 | 11 | #output { 12 | padding: 20px; 13 | } 14 | -------------------------------------------------------------------------------- /futures/sequential-processing/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /futures/sequential-processing/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /futures/sequential-processing/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: sequential_processing 2 | description: A sample application that performs time consuming operations sequentially. 3 | homepage: https://dart.dev/tutorials/language/futures 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /futures/sequential-processing/web/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-tutorials-samples/111938d000c5204e31e80be9cf4128739146aa0d/futures/sequential-processing/web/favicon.ico -------------------------------------------------------------------------------- /futures/sequential-processing/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | sequential_processing 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /futures/sequential-processing/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'dart:async'; 6 | import 'dart:html'; 7 | 8 | void main() { 9 | expensiveA() 10 | .then((aValue) => expensiveB()) 11 | .then((bValue) => expensiveC()) 12 | .then((cValue) => doSomethingWith(cValue)); 13 | } 14 | 15 | Future expensiveA() => Future.value('from expensiveA'); 16 | Future expensiveB() => Future.value('from expensiveB'); 17 | Future expensiveC() => Future.value('from expensiveC'); 18 | 19 | void doSomethingWith(value) { 20 | querySelector('#output').appendText(value); 21 | } 22 | -------------------------------------------------------------------------------- /futures/sequential-processing/web/styles.css: -------------------------------------------------------------------------------- 1 | @import url(https://fonts.googleapis.com/css?family=Roboto); 2 | 3 | html, body { 4 | width: 100%; 5 | height: 100%; 6 | margin: 0; 7 | padding: 0; 8 | font-family: 'Roboto', sans-serif; 9 | } 10 | 11 | #output { 12 | padding: 20px; 13 | } 14 | -------------------------------------------------------------------------------- /futures/waiting-on-futures/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /futures/waiting-on-futures/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /futures/waiting-on-futures/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: waiting_on_futures 2 | description: A sample application that triggers three functions and waits until all are finished. 3 | homepage: https://dart.dev/tutorials/language/futures 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /futures/waiting-on-futures/web/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dart-archive/dart-tutorials-samples/111938d000c5204e31e80be9cf4128739146aa0d/futures/waiting-on-futures/web/favicon.ico -------------------------------------------------------------------------------- /futures/waiting-on-futures/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | waiting_on_futures 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /futures/waiting-on-futures/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'dart:async'; 6 | import 'dart:html'; 7 | 8 | final output = querySelector('#output'); 9 | 10 | void main() { 11 | Future.wait([expensiveA(), expensiveB(), expensiveC()]) 12 | .then((List responses) => chooseBestResponse(responses)) 13 | .catchError((e) => handleError(e)); 14 | } 15 | 16 | Future expensiveA() => Future.value('from expensiveA'); 17 | Future expensiveB() => Future.value('from expensiveB'); 18 | Future expensiveC() => Future.value('from expensiveC'); 19 | 20 | void chooseBestResponse(List responses) { 21 | output.appendText(responses[1]); 22 | } 23 | 24 | void handleError(e) { 25 | output.appendText('handle error: $e'); 26 | } 27 | -------------------------------------------------------------------------------- /futures/waiting-on-futures/web/styles.css: -------------------------------------------------------------------------------- 1 | @import url(https://fonts.googleapis.com/css?family=Roboto); 2 | 3 | html, body { 4 | width: 100%; 5 | height: 100%; 6 | margin: 0; 7 | padding: 0; 8 | font-family: 'Roboto', sans-serif; 9 | } 10 | 11 | #output { 12 | padding: 20px; 13 | } 14 | -------------------------------------------------------------------------------- /httpserver/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | 11 | # Content written out by bin/basic_writer_server.dart 12 | file.txt 13 | -------------------------------------------------------------------------------- /httpserver/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /httpserver/bin/basic_file_server.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // Uses the http_server package. 6 | // Similar to mini_file_server.dart, which uses straight Dart APIs. 7 | // For all requests serves index.html in the same directory 8 | // as this script. 9 | // Also see static_file_server.dart. 10 | 11 | import 'dart:async'; 12 | import 'dart:io'; 13 | import 'package:http_server/http_server.dart'; 14 | import 'package:path/path.dart' as p; 15 | 16 | var targetFile = 17 | File(p.join(p.dirname(Platform.script.toFilePath()), 'index.html')); 18 | 19 | Future main() async { 20 | var staticFiles = VirtualDirectory('.'); 21 | 22 | var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 4046); 23 | print('Listening on http://${server.address.address}:${server.port}/'); 24 | await for (var request in server) { 25 | staticFiles.serveFile(targetFile, request); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /httpserver/bin/basic_writer_client.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // Client to basic_writer_server.dart. 6 | // Makes a POST request containing JSON-encoded data 7 | // to the server and prints the response. 8 | 9 | import 'dart:async'; 10 | import 'dart:convert'; 11 | import 'dart:io'; 12 | 13 | String _host = InternetAddress.loopbackIPv4.host; 14 | String path = 'file.txt'; 15 | 16 | Map jsonData = { 17 | 'name': 'Han Solo', 18 | 'job': 'reluctant hero', 19 | 'BFF': 'Chewbacca', 20 | 'ship': 'Millennium Falcon', 21 | 'weakness': 'smuggling debts' 22 | }; 23 | 24 | Future main() async { 25 | final request = await HttpClient().post(_host, 4049, path) /*1*/ 26 | ..headers.contentType = ContentType.json /*2*/ 27 | ..write(jsonEncode(jsonData)); /*3*/ 28 | final response = await request.close(); /*4*/ 29 | await utf8.decoder.bind(response /*5*/).forEach(print); 30 | } 31 | -------------------------------------------------------------------------------- /httpserver/bin/basic_writer_server.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // Server to basic_writer_client.dart. 6 | // Receives JSON encoded data in a POST request and writes it to 7 | // the file specified in the URI. 8 | 9 | import 'dart:async'; 10 | import 'dart:convert'; 11 | import 'dart:io'; 12 | 13 | String _host = InternetAddress.loopbackIPv4.host; 14 | 15 | Future main() async { 16 | var server = await HttpServer.bind(_host, 4049); 17 | print('Listening on http://${server.address.address}:${server.port}/'); 18 | await for (var req in server) { 19 | final contentType = req.headers.contentType; 20 | final response = req.response; 21 | 22 | if (req.method == 'POST' && 23 | contentType?.mimeType == 'application/json' /*1*/) { 24 | try { 25 | final content = await utf8.decoder.bind(req).join(); /*2*/ 26 | var data = jsonDecode(content) as Map; /*3*/ 27 | var fileName = req.uri.pathSegments.last; /*4*/ 28 | await File(fileName).writeAsString(content, mode: FileMode.write); 29 | req.response 30 | ..statusCode = HttpStatus.ok 31 | ..write('Wrote data for ${data['name']}.'); 32 | } catch (e) { 33 | response 34 | ..statusCode = HttpStatus.internalServerError 35 | ..write('Exception during file I/O: $e.'); 36 | } 37 | } else { 38 | response 39 | ..statusCode = HttpStatus.methodNotAllowed 40 | ..write('Unsupported request: ${req.method}.'); 41 | } 42 | await response.close(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /httpserver/bin/hello_world_server.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // Replies "Hello, world!" to all requests. 6 | // Use the URL localhost:4040 in your browser. 7 | import 'dart:io'; 8 | import 'dart:async'; 9 | 10 | Future main() async { 11 | var server = await HttpServer.bind( 12 | InternetAddress.loopbackIPv4, 13 | 4040, 14 | ); 15 | print('Listening on http://${server.address.address}:${server.port}/'); 16 | 17 | await for (HttpRequest request in server) { 18 | request.response.write('Hello, world!'); 19 | await request.response.close(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /httpserver/bin/hello_world_server_secure.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // Replies "Hello, world!" to all requests. 6 | // Use the HTTPS URL https://localhost:4047 in your browser. 7 | // This sample requires Dart version 1.13 or later. 8 | // You might get a warning about a potential security risk, because the 9 | // server certificate is signed by a test authority. You can add the 10 | // server_authority.pem certificate to your test browser as a trusted 11 | // authority to avoid this message, but that may make that browser less secure. 12 | 13 | import 'dart:async'; 14 | import 'dart:io'; 15 | import 'package:path/path.dart' as p; 16 | 17 | String certificateChain = 'server_chain.pem'; 18 | String serverKey = 'server_key.pem'; 19 | 20 | Future main() async { 21 | Directory.current = p.dirname(Platform.script.toFilePath()); 22 | var serverContext = SecurityContext(); /*1*/ 23 | serverContext.useCertificateChain(certificateChain); /*2*/ 24 | serverContext.usePrivateKey(serverKey, password: 'dartdart'); /*3*/ 25 | 26 | var server = await HttpServer.bindSecure( 27 | InternetAddress.loopbackIPv4, 28 | 4047, 29 | serverContext, /*4*/ 30 | ); 31 | print('Listening on https://${server.address.address}:${server.port}/'); 32 | await for (HttpRequest request in server) { 33 | request.response.write('Hello, world!'); 34 | await request.response.close(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /httpserver/bin/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 12 | 13 | 14 | 15 | 16 | 17 | Hello, world! 18 | 19 | 20 |

Hello, World!

21 | At your service ... 22 | 23 | 24 | -------------------------------------------------------------------------------- /httpserver/bin/mini_file_server.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // Serves the index.html in the same directory as this script. 6 | // Use the URL localhost:4044 in your browser. 7 | // For a similar server that uses http_server package 8 | // see basic_file_server.dart. 9 | 10 | import 'dart:async'; 11 | import 'dart:io'; 12 | 13 | import 'package:path/path.dart' as p; 14 | 15 | var targetFile = 16 | File(p.join(p.dirname(Platform.script.toFilePath()), 'index.html')); 17 | 18 | Future main() async { 19 | var server; 20 | 21 | try { 22 | server = await HttpServer.bind(InternetAddress.loopbackIPv4, 4044); 23 | } catch (e) { 24 | print("Couldn't bind to port 4044: $e"); 25 | exit(-1); 26 | } 27 | print('Listening on http://${server.address.address}:${server.port}/'); 28 | 29 | await for (HttpRequest req in server) { 30 | if (await targetFile.exists()) { 31 | print("Serving ${targetFile.path}."); 32 | req.response.headers.contentType = ContentType.html; 33 | try { 34 | await req.response.addStream(targetFile.openRead()); 35 | } catch (e) { 36 | print("Couldn't read file: $e"); 37 | exit(-1); 38 | } 39 | } else { 40 | print("Can't open ${targetFile.path}."); 41 | req.response.statusCode = HttpStatus.notFound; 42 | } 43 | await req.response.close(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /httpserver/bin/note_server.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // Client program is note_client.dart. 6 | // Use note_taker.html to run the client. 7 | 8 | import 'dart:async'; 9 | import 'dart:convert' show utf8, json; 10 | import 'dart:io'; 11 | 12 | import 'package:path/path.dart' as p; 13 | 14 | int count = 0; 15 | 16 | Future main() async { 17 | Directory.current = p.dirname(Platform.script.toFilePath()); 18 | 19 | // One note per line. 20 | try { 21 | final lines = File('notes.txt').readAsLinesSync(); 22 | count = lines.length; 23 | } on FileSystemException { 24 | print('Could not open notes.txt.'); 25 | return; 26 | } 27 | 28 | var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 4042); 29 | print('Listening on http://${server.address.address}:${server.port}/'); 30 | await listenForRequests(server); 31 | } 32 | 33 | Future listenForRequests(HttpServer requests) async { 34 | await for (HttpRequest request in requests) { 35 | switch (request.method) { 36 | case 'POST': 37 | await handlePost(request); 38 | break; 39 | case 'OPTION': 40 | handleOptions(request); 41 | break; 42 | default: 43 | defaultHandler(request); 44 | break; 45 | } 46 | } 47 | print('No more requests.'); 48 | } 49 | 50 | Future handlePost(HttpRequest request) async { 51 | Map decoded; 52 | 53 | addCorsHeaders(request.response); 54 | 55 | try { 56 | decoded = 57 | await utf8.decoder.bind(request).transform(json.decoder).first as Map; 58 | } catch (e) { 59 | print('Request listen error: $e'); 60 | return; 61 | } 62 | 63 | if (decoded.containsKey('myNote')) { 64 | saveNote(request, "${decoded['myNote']}\n"); 65 | } else { 66 | getNote(request, decoded['getNote'] as String); 67 | } 68 | } 69 | 70 | void saveNote(HttpRequest request, String myNote) { 71 | try { 72 | File('notes.txt').writeAsStringSync(myNote, mode: FileMode.append); 73 | } catch (e) { 74 | print('Couldn\'t open notes.txt: $e'); 75 | request.response 76 | ..statusCode = HttpStatus.internalServerError 77 | ..writeln('Couldn\'t save note.') 78 | ..close(); 79 | return; 80 | } 81 | 82 | count++; 83 | request.response 84 | ..statusCode = HttpStatus.ok 85 | ..writeln('You have $count notes.') 86 | ..close(); 87 | } 88 | 89 | void getNote(HttpRequest request, String getNote) { 90 | final requestedNote = int.tryParse(getNote) ?? 0; 91 | if (requestedNote >= 0 && requestedNote < count) { 92 | final lines = File('notes.txt').readAsLinesSync(); 93 | request.response 94 | ..statusCode = HttpStatus.ok 95 | ..writeln(lines[requestedNote]) 96 | ..close(); 97 | } 98 | } 99 | 100 | void defaultHandler(HttpRequest request) { 101 | final response = request.response; 102 | addCorsHeaders(response); 103 | response 104 | ..statusCode = HttpStatus.notFound 105 | ..write('Not found: ${request.method}, ${request.uri.path}') 106 | ..close(); 107 | } 108 | 109 | void handleOptions(HttpRequest request) { 110 | final response = request.response; 111 | addCorsHeaders(response); 112 | print('${request.method}: ${request.uri.path}'); 113 | response 114 | ..statusCode = HttpStatus.noContent 115 | ..close(); 116 | } 117 | 118 | void addCorsHeaders(HttpResponse response) { 119 | response.headers.add('Access-Control-Allow-Origin', '*'); 120 | response.headers.add('Access-Control-Allow-Methods', 'POST, OPTIONS'); 121 | response.headers.add('Access-Control-Allow-Headers', 122 | 'Origin, X-Requested-With, Content-Type, Accept'); 123 | } 124 | -------------------------------------------------------------------------------- /httpserver/bin/notes.txt: -------------------------------------------------------------------------------- 1 | Be yourself. Everyone else is taken. 2 | Don't cry because it's over, smile because it happened. 3 | A person's a person, no matter how small. 4 | Enjoy all your meals. 5 | Sing. Dance. Play. 6 | -------------------------------------------------------------------------------- /httpserver/bin/number_guesser.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // Automatic client to number_thinker.dart. 6 | 7 | import 'dart:async'; 8 | import 'dart:convert'; 9 | import 'dart:io'; 10 | import 'dart:math'; 11 | 12 | Duration oneSecond = Duration(seconds: 1); 13 | Random myRandomGenerator = Random(); 14 | HttpClient client = HttpClient(); 15 | 16 | Future main() async { 17 | // Delay successive guesses by oneSecond. 18 | final guesses = Stream.periodic(oneSecond, (_) => guess()); 19 | 20 | // Guess until we get it right 21 | await for (final guess in guesses) { 22 | if (await guess) break; 23 | } 24 | } 25 | 26 | Future guess() { 27 | final guess = myRandomGenerator.nextInt(10); 28 | return checkGuess(guess); 29 | } 30 | 31 | Future checkGuess(int guess) async { 32 | var isGoodGuess = false; 33 | final request = 34 | await client.get(InternetAddress.loopbackIPv4.host, 4041, '/?q=$guess'); 35 | print('Guess is $guess.'); 36 | final response = await request.close(); 37 | if (response.statusCode == HttpStatus.ok) { 38 | var contents = await utf8.decoder.bind(response).join(); 39 | if (contents.startsWith('true')) { 40 | isGoodGuess = true; 41 | client.close(); 42 | print('Good guess, yay!'); 43 | } else { 44 | print('Bad guess, trying again.'); 45 | } 46 | } 47 | return isGoodGuess; 48 | } 49 | -------------------------------------------------------------------------------- /httpserver/bin/number_thinker.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // Use the client program, number_guesser.dart to automatically make guesses. 6 | // Or, you can manually guess the number using the URL localhost:4045/?q=#, 7 | // where # is your guess. 8 | // Or, you can use the make_a_guess.html UI. 9 | 10 | import 'dart:async'; 11 | import 'dart:io'; 12 | import 'dart:math' show Random; 13 | 14 | Random intGenerator = Random(); 15 | int myNumber = intGenerator.nextInt(10); 16 | 17 | Future main() async { 18 | print("I'm thinking of a number: $myNumber"); 19 | 20 | final server = await HttpServer.bind( 21 | InternetAddress.loopbackIPv4, 22 | 4041, 23 | ); 24 | print('Listening on http://${server.address.address}:${server.port}/'); 25 | await for (var request in server) { 26 | handleRequest(request); 27 | } 28 | } 29 | 30 | void handleRequest(HttpRequest request) { 31 | try { 32 | if (request.method == 'GET') { 33 | handleGet(request); 34 | } else { 35 | request.response 36 | ..statusCode = HttpStatus.methodNotAllowed 37 | ..write('Unsupported request: ${request.method}.') 38 | ..close(); 39 | } 40 | } catch (e) { 41 | print('Exception in handleRequest: $e'); 42 | } 43 | print('Request handled.'); 44 | } 45 | 46 | void handleGet(HttpRequest request) { 47 | final guess = request.uri.queryParameters['q']; 48 | final response = request.response; 49 | response.statusCode = HttpStatus.ok; 50 | if (guess == myNumber.toString()) { 51 | response 52 | ..writeln('true') 53 | ..writeln("I'm thinking of another number.") 54 | ..close(); 55 | myNumber = intGenerator.nextInt(10); 56 | print("I'm thinking of another number: $myNumber"); 57 | } else { 58 | response 59 | ..writeln('false') 60 | ..close(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /httpserver/bin/server_authority.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIC+zCCAeOgAwIBAgIBATANBgkqhkiG9w0BAQsFADAYMRYwFAYDVQQDDA1yb290 3 | YXV0aG9yaXR5MB4XDTE1MTAyNzEwMjYzNFoXDTI1MTAyNDEwMjYzNFowGDEWMBQG 4 | A1UEAwwNcm9vdGF1dGhvcml0eTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC 5 | ggEBAMl+dcraUM/E7E6zl7+7hK9oUJYXJLnfiMtP/TRFVbH4+2aEN8vXzPbzKdR3 6 | FfaHczXQTwnTCaYA4u4uSDvSOsFFEfxEwYORsdKmQEM8nGpVX2NVvKsMcGIhh8kh 7 | ZwJfkMIOcAxmGIHGdMhF8VghonJ8uGiuqktxdfpARq0g3fqIjDHsF9/LpfshUfk9 8 | wsRyTF0yr90U/dsfnE+u8l7GvVl8j2Zegp0sagAGtLaNv7tP17AibqEGg2yDBrBN 9 | 9r9ihe4CqMjx+Q2kQ2S9Gz2V2ReO/n6vm2VQxsPRB/lV/9jh7cUcS0/9mggLYrDy 10 | cq1v7rLLQrWuxMz1E3gOhyCYJ38CAwEAAaNQME4wHQYDVR0OBBYEFHEPkNAdOa2k 11 | MUoM6Ix/kUCLq99vMB8GA1UdIwQYMBaAFHEPkNAdOa2kMUoM6Ix/kUCLq99vMAwG 12 | A1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBABrhjnWC6b+z9Kw73C/niOwo 13 | 9sPdufjS6tb0sCwDjt3mjvE4NdNWt+/+ZOugW6dqtvqhtqZM1q0u9pJkNwIrqgFD 14 | ZHcfNaf31G6Z2YE+Io7woTVw6fFobg/EFo+a/qwbvWL26McmiRL5yiSBjVjpX4a5 15 | kdZ+aPQUCBaLrTWwlCDqzSVIULWUQvveRWbToMFKPNID58NtEpymAx3Pgir7YjV9 16 | UnlU2l5vZrh1PTCqZxvC/IdRESUfW80LdHaeyizRUP+6vKxGgSz2MRuYINjbd6GO 17 | hGiCpWlwziW2xLV1l2qSRLko2kIafLZP18N0ThM9zKbU5ps9NgFOf//wqSGtLaE= 18 | -----END CERTIFICATE----- 19 | -------------------------------------------------------------------------------- /httpserver/bin/server_chain.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDZDCCAkygAwIBAgIBATANBgkqhkiG9w0BAQsFADAgMR4wHAYDVQQDDBVpbnRl 3 | cm1lZGlhdGVhdXRob3JpdHkwHhcNMTUxMDI3MTAyNjM1WhcNMjUxMDI0MTAyNjM1 4 | WjAUMRIwEAYDVQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw 5 | ggEKAoIBAQCkg/Qr8RQeLTOSgCkyiEX2ztgkgscX8hKGHEHdvlkmVK3JVEIIwkvu 6 | /Y9LtHZUia3nPAgqEEbexzTENZjSCcC0V6I2XW/e5tIE3rO0KLZyhtZhN/2SfJ6p 7 | KbOh0HLr1VtkKJGp1tzUmHW/aZI32pK60ZJ/N917NLPCJpCaL8+wHo3+w3oNqln6 8 | oJsfgxy9SUM8Bsc9WMYKMUdqLO1QKs1A5YwqZuO7Mwj+4LY2QDixC7Ua7V9YAPo2 9 | 1SBeLvMCHbYxSPCuxcZ/kDkgax/DF9u7aZnGhMImkwBka0OQFvpfjKtTIuoobTpe 10 | PAG7MQYXk4RjnjdyEX/9XAQzvNo1CDObAgMBAAGjgbQwgbEwPAYDVR0RBDUwM4IJ 11 | bG9jYWxob3N0ggkxMjcuMC4wLjGCAzo6MYcEfwAAAYcQAAAAAAAAAAAAAAAAAAAA 12 | ATAMBgNVHRMBAf8EAjAAMB0GA1UdDgQWBBSvhJo6taTggJQBukEvMo/PDk8tKTAf 13 | BgNVHSMEGDAWgBS98L4T5RaIToE3DkBRsoeWPil0eDAOBgNVHQ8BAf8EBAMCA6gw 14 | EwYDVR0lBAwwCgYIKwYBBQUHAwEwDQYJKoZIhvcNAQELBQADggEBAHLOt0mL2S4A 15 | B7vN7KsfQeGlVgZUVlEjem6kqBh4fIzl4CsQuOO8oJ0FlO1z5JAIo98hZinymJx1 16 | phBVpyGIKakT/etMH0op5evLe9dD36VA3IM/FEv5ibk35iGnPokiJXIAcdHd1zam 17 | YaTHRAnZET5S03+7BgRTKoRuszhbvuFz/vKXaIAnVNOF4Gf2NUJ/Ax7ssJtRkN+5 18 | UVxe8TZVxzgiRv1uF6NTr+J8PDepkHCbJ6zEQNudcFKAuC56DN1vUe06gRDrNbVq 19 | 2JHEh4pRfMpdsPCrS5YHBjVq/XHtFHgwDR6g0WTwSUJvDeM4OPQY5f61FB0JbFza 20 | PkLkXmoIod8= 21 | -----END CERTIFICATE----- 22 | -----BEGIN CERTIFICATE----- 23 | MIIDLjCCAhagAwIBAgIBAjANBgkqhkiG9w0BAQsFADAYMRYwFAYDVQQDDA1yb290 24 | YXV0aG9yaXR5MB4XDTE1MTAyNzEwMjYzNVoXDTI1MTAyNDEwMjYzNVowIDEeMBwG 25 | A1UEAwwVaW50ZXJtZWRpYXRlYXV0aG9yaXR5MIIBIjANBgkqhkiG9w0BAQEFAAOC 26 | AQ8AMIIBCgKCAQEA6GndRFiXk+2q+Ig7ZOWKKGta+is8137qyXz+eVFs5sA0ajMN 27 | ZBAMWS0TIXw/Yks+y6fEcV/tfv91k1eUN4YXPcoxTdDF97d2hO9wxumeYOMnQeDy 28 | VZVDKQBZ+jFMeI+VkNpMEdmsLErpZDGob/1dC8tLEuR6RuRR8X6IDGMPOCMw1jLK 29 | V1bQjPtzqKadTscfjLuKxuLgspJdTrzsu6hdcl1mm8K6CjTY2HNXWxs1yYmwfuQ2 30 | Z4/8sOMNqFqLjN+ChD7pksTMq7IosqGiJzi2bpd5f44ek/k822Y0ATncJHk4h1Z+ 31 | kZBnW6kgcLna1gDri9heRwSZ+M8T8nlHgIMZIQIDAQABo3sweTASBgNVHRMBAf8E 32 | CDAGAQH/AgEAMB0GA1UdDgQWBBS98L4T5RaIToE3DkBRsoeWPil0eDAfBgNVHSME 33 | GDAWgBRxD5DQHTmtpDFKDOiMf5FAi6vfbzAOBgNVHQ8BAf8EBAMCAgQwEwYDVR0l 34 | BAwwCgYIKwYBBQUHAwEwDQYJKoZIhvcNAQELBQADggEBAD+4KpUeV5mUPw5IG/7w 35 | eOXnUpeS96XFGuS1JuFo/TbgntPWSPyo+rD4GrPIkUXyoHaMCDd2UBEjyGbBIKlB 36 | NZA3RJOAEp7DTkLNK4RFn/OEcLwG0J5brL7kaLRO4vwvItVIdZ2XIqzypRQTc0MG 37 | MmF08zycnSlaN01ryM67AsMhwdHqVa+uXQPo8R8sdFGnZ33yywTYD73FeImXilQ2 38 | rDnFUVqmrW1fjl0Fi4rV5XI0EQiPrzKvRtmF8ZqjGATPOsRd64cwQX6V+P5hNeIR 39 | 9pba6td7AbNGausHfacRYMyoGJWWWkFPd+7jWOCPqW7Fk1tmBgdB8GzXa3inWIRM 40 | RUE= 41 | -----END CERTIFICATE----- 42 | -----BEGIN CERTIFICATE----- 43 | MIIC+zCCAeOgAwIBAgIBATANBgkqhkiG9w0BAQsFADAYMRYwFAYDVQQDDA1yb290 44 | YXV0aG9yaXR5MB4XDTE1MTAyNzEwMjYzNFoXDTI1MTAyNDEwMjYzNFowGDEWMBQG 45 | A1UEAwwNcm9vdGF1dGhvcml0eTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC 46 | ggEBAMl+dcraUM/E7E6zl7+7hK9oUJYXJLnfiMtP/TRFVbH4+2aEN8vXzPbzKdR3 47 | FfaHczXQTwnTCaYA4u4uSDvSOsFFEfxEwYORsdKmQEM8nGpVX2NVvKsMcGIhh8kh 48 | ZwJfkMIOcAxmGIHGdMhF8VghonJ8uGiuqktxdfpARq0g3fqIjDHsF9/LpfshUfk9 49 | wsRyTF0yr90U/dsfnE+u8l7GvVl8j2Zegp0sagAGtLaNv7tP17AibqEGg2yDBrBN 50 | 9r9ihe4CqMjx+Q2kQ2S9Gz2V2ReO/n6vm2VQxsPRB/lV/9jh7cUcS0/9mggLYrDy 51 | cq1v7rLLQrWuxMz1E3gOhyCYJ38CAwEAAaNQME4wHQYDVR0OBBYEFHEPkNAdOa2k 52 | MUoM6Ix/kUCLq99vMB8GA1UdIwQYMBaAFHEPkNAdOa2kMUoM6Ix/kUCLq99vMAwG 53 | A1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBABrhjnWC6b+z9Kw73C/niOwo 54 | 9sPdufjS6tb0sCwDjt3mjvE4NdNWt+/+ZOugW6dqtvqhtqZM1q0u9pJkNwIrqgFD 55 | ZHcfNaf31G6Z2YE+Io7woTVw6fFobg/EFo+a/qwbvWL26McmiRL5yiSBjVjpX4a5 56 | kdZ+aPQUCBaLrTWwlCDqzSVIULWUQvveRWbToMFKPNID58NtEpymAx3Pgir7YjV9 57 | UnlU2l5vZrh1PTCqZxvC/IdRESUfW80LdHaeyizRUP+6vKxGgSz2MRuYINjbd6GO 58 | hGiCpWlwziW2xLV1l2qSRLko2kIafLZP18N0ThM9zKbU5ps9NgFOf//wqSGtLaE= 59 | -----END CERTIFICATE----- 60 | -------------------------------------------------------------------------------- /httpserver/bin/server_key.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN ENCRYPTED PRIVATE KEY----- 2 | MIIE4zAcBgoqhkiG9w0BDAEBMA4ECBMCjlg8JYZ4AgIIAASCBMFd9cBoZ5xcTock 3 | AVQcg/HzYJtMceKn1gtMDdC7mmXuyN0shoxhG4BpQInHkFARL+nenesXFxEm4X5e 4 | L603Pcgw72/ratxVpTW7hPMjiLTEBqza0GjQm7Sarbdy+Vzdp/6XFrAcPfFl1juY 5 | oyYzbozPsvFHz3Re44y1KmI4HAzU/qkjJUbNTTiPPVI2cDP6iYN2XXxBb1wwp8jR 6 | iqdZqFG7lU/wvPEbD7BVPpmJBHWNG681zb4ea5Zn4hW8UaxpiIBiaH0/IWc2SVZd 7 | RliAFo3NEsGxCcsnBo/n00oudGbOJxdOp7FbH5hJpeqX2WhCyJRxIeHOWmeuMAet 8 | 03HFriiEmJ99m2nEJN1x0A3QUUM7ji6vZAb4qb1dyq7LlX4M2aaqixRnaTcQkapf 9 | DOxX35DEBXSKrDpyWp6Rx4wNpUyi1TKyhaVnYgD3Gn0VfC/2w86gSFlrf9PMYGM0 10 | PvFxTDzTyjOuPBRa728gZOGXgDOL7qvdInU/opVew7kFeRQHXxHzFCLK5dD+Vrig 11 | 5fS3m0++f55ODkxqHXB8gbXbd3GMmsW6MrGpU7VsCNtbVPdSMW0FalovEB0M+2lj 12 | 1VfuvL+0F5huTe+BgZAt6xgET/CIcZXdNMRPVhraqUjqWtI9Rdk4STPCpU1rDkjG 13 | YDl/fo4W2T6qQWFUpiC9IvVVGkVxaqfZZ4Qu+V5xPUi6vk95QiTNkN1t+m+sCCgS 14 | Llkea8Um0aHMy33Lj3NsfL0LMrnpniqcAks8BvcgIZwk1VRqcj7BQVCygJSYrmAR 15 | DBhMpjWlXuSggnyVPuduZDtnTN+8lCHLOKL3a3bDb6ySaKX49Km6GutDLfpDtEA0 16 | 3mQvmEG4XVm7zy+AlN72qFbtSLDRi/D/uQh2q/ZrFQLOBQBQB56TvEbKouLimUDM 17 | ascQA3aUyhOE7e+d02NOFIFTozwc/C//CIFeA+ZEwxyfha/3Bor6Jez7PC/eHNxZ 18 | w7YMXzPW9NhcCcerhYGebuCJxLwzqJ+IGdukjKsGV2ytWDoB2xZiJNu096j4RKcq 19 | YSJoen0R7IH8N4eDujXR8m9kAl724Uqs1OoAs4VNICvzTutbsgVZ6Z+NMOcfnPw9 20 | jZkFhot16w8znD+OmhBR7/bzOLpaeUhk7EhNq5M6U0NNWx3WwkDlvU/jx+6/EQe3 21 | iLEHptH2HYBF1xscaKGbtKNtuQsfdzgWpOX0qK2YbK3yCKvL/xIm1DQmDZDKkWdW 22 | VNh8oGV1H96CivWlvxhAgXKz9F/83CjMw8YXRk7RJvWR4vtNvXFAvGkFIYCN9Jv9 23 | p+1ukaYoxSLGBik907I6gWSHqumJiCprUyAX/bVfZfNiYh4hzeA3lhwxZSax3JG4 24 | 7QFPvyepOmF/3AAzS/Pusx6jOZnuCMCkfQi6Wpem1o3s4x+fP7kz00Xuj01ErucM 25 | S10ixfIh84kXBN3dTRDtDdeCyoMsBKO0W5jDBBlWL02YfdF6Opo1Q4cPh2DYgXMh 26 | XEszNZSK5LB0y+f3A6Kdx/hkZzHVvMONA70OyrkoZzGyWENhcB0c7ntTJyPPD2qM 27 | s0HRA2VwF/0ypU3OKERM1Ua5NSkTgvnnVTlV9GO90Tkn5v4fxdl8NzIuJLyGguTP 28 | Xc0tRM34Lg== 29 | -----END ENCRYPTED PRIVATE KEY----- 30 | -------------------------------------------------------------------------------- /httpserver/bin/static_file_server.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // Type localhost:4048 into your browser. 6 | // This server returns the contents of index.html for all requests. 7 | 8 | import 'dart:async'; 9 | import 'dart:io'; 10 | import 'package:http_server/http_server.dart'; 11 | import 'package:path/path.dart' as p; 12 | 13 | Future main() async { 14 | var pathToBuild = p.join(p.dirname(Platform.script.toFilePath())); 15 | 16 | var staticFiles = VirtualDirectory(pathToBuild); 17 | staticFiles.allowDirectoryListing = true; /*1*/ 18 | staticFiles.directoryHandler = (dir, request) /*2*/ { 19 | var indexUri = Uri.file(dir.path).resolve('index.html'); 20 | staticFiles.serveFile(File(indexUri.toFilePath()), request); /*3*/ 21 | }; 22 | 23 | var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 4048); 24 | print('Listening on http://${server.address.address}:${server.port}/'); 25 | await server.forEach(staticFiles.serveRequest); /*4*/ 26 | } 27 | -------------------------------------------------------------------------------- /httpserver/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: httpserver 2 | description: Sample HTTP clients and servers 3 | homepage: https://dart.dev/tutorials/server/httpserver 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dependencies: 9 | http_server: ^0.9.8+1 10 | path: ^1.6.2 11 | 12 | dev_dependencies: 13 | test: ^1.6.3 14 | pedantic: ^1.7.0 15 | build_runner: ^1.5.0 16 | build_web_compilers: ^2.1.0 17 | -------------------------------------------------------------------------------- /httpserver/test/httpserver_test.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | import 'dart:convert'; 3 | import 'dart:io'; 4 | 5 | import 'package:test/test.dart'; 6 | 7 | import '../bin/basic_file_server.dart' as basic_file_server; 8 | import '../bin/basic_writer_client.dart' as basic_writer_client; 9 | import '../bin/basic_writer_server.dart' as basic_writer_server; 10 | import '../bin/hello_world_server.dart' as hello_world_server; 11 | import '../bin/hello_world_server_secure.dart' as hello_world_server_secure; 12 | import '../bin/mini_file_server.dart' as mini_file_server; 13 | import '../bin/number_guesser.dart' as number_guesser; 14 | import '../bin/number_thinker.dart' as number_thinker; 15 | 16 | void main() { 17 | test('hello_world_server', () { 18 | const port = 4040; 19 | 20 | _test() async { 21 | expect(await getUrl('localhost', port), 'Hello, world!'); 22 | } 23 | 24 | expect( 25 | () => Future.any([ 26 | hello_world_server.main(), 27 | _test(), 28 | ]), 29 | prints(startsWith('Listening on http://127.0.0.1:$port'))); 30 | }); 31 | 32 | group('number_thinker and number_guesser:', () { 33 | // Only resolve the server once for all tests in this group to avoid 34 | // "binding multiple times on the same (address, port) combination". 35 | Future _server; 36 | Future getServer() => _server ??= number_thinker.main(); 37 | 38 | test('number_thinker response', () { 39 | _test() async { 40 | expect(await getUrl('localhost', 4041), anyOf('true\n', 'false\n')); 41 | } 42 | 43 | expect( 44 | () => Future.any([ 45 | getServer(), 46 | _test(), 47 | ]), 48 | prints(allOf( 49 | startsWith("I'm thinking of a number:"), 50 | contains('Request handled.'), 51 | ))); 52 | }); 53 | 54 | test('client bad guess', () async { 55 | expect( 56 | () => Future.any([ 57 | getServer(), 58 | number_guesser.checkGuess(99), 59 | ]), 60 | prints(allOf( 61 | contains('Guess is 99.'), 62 | contains('Bad guess'), 63 | ))); 64 | }); 65 | 66 | test('client good guess', () async { 67 | _test() async { 68 | // For now, guess each number in turn 69 | // (rather than having to mock the int generator). 70 | for (var i = 0; i < 10; i++) { 71 | if (await number_guesser.checkGuess(i)) return; 72 | } 73 | } 74 | 75 | expect( 76 | () => Future.any([ 77 | getServer(), 78 | _test(), 79 | ]), 80 | prints(contains('Good guess'))); 81 | }); 82 | }); 83 | 84 | group('basic_writer_client and server:', () { 85 | final file = File('file.txt'); 86 | final port = 4049; 87 | 88 | // Only resolve the server once for all tests in this group to avoid 89 | // "binding multiple times on the same (address, port) combination". 90 | final server = basic_writer_server.main(); 91 | 92 | void deleteTmpFile() { 93 | if (file.existsSync()) file.delete(); 94 | } 95 | 96 | setUp(deleteTmpFile); 97 | tearDown(deleteTmpFile); 98 | 99 | test('GET', () async { 100 | _test() async { 101 | expect(await getUrl('localhost', port), 'Unsupported request: GET.'); 102 | } 103 | 104 | expect( 105 | () async => Future.any([ 106 | server, 107 | _test(), 108 | ]), 109 | prints(''), 110 | ); 111 | }); 112 | 113 | test('basic_writer_client and server', () async { 114 | final expectedJson = 115 | '{"name":"Han Solo","job":"reluctant hero","BFF":"Chewbacca",' 116 | '"ship":"Millennium Falcon","weakness":"smuggling debts"}'; 117 | 118 | _test() async { 119 | // Test POST 120 | await basic_writer_client.main(); 121 | expect(expectedJson, file.readAsStringSync()); 122 | } 123 | 124 | expect( 125 | () async => Future.any([ 126 | server, 127 | _test(), 128 | ]), 129 | prints('Wrote data for Han Solo.\n'), 130 | ); 131 | }); 132 | }); 133 | 134 | test('mini_file_server', () { 135 | final file = File('bin/index.html'); 136 | 137 | _server() { 138 | mini_file_server.targetFile = file; 139 | return mini_file_server.main(); 140 | } 141 | 142 | _test() async { 143 | expect(await getUrl('localhost', 4044), file.readAsStringSync()); 144 | } 145 | 146 | expect( 147 | () => Future.any([ 148 | _server(), 149 | _test(), 150 | ]), 151 | prints('Listening on http://127.0.0.1:4044/\nServing ${file.path}.\n')); 152 | }); 153 | 154 | test('basic_file_server', () async { 155 | final file = File('bin/index.html'); 156 | 157 | _server() { 158 | basic_file_server.targetFile = file; 159 | return basic_file_server.main(); 160 | } 161 | 162 | _test() async { 163 | expect(await getUrl('localhost', 4046), file.readAsStringSync()); 164 | } 165 | 166 | await Future.any([ 167 | _server(), 168 | _test(), 169 | ]); 170 | }); 171 | 172 | test('hello_world_server_secure', () { 173 | const port = 4047; 174 | 175 | _server() { 176 | hello_world_server_secure.certificateChain = 177 | 'bin/' + hello_world_server_secure.certificateChain; 178 | hello_world_server_secure.serverKey = 179 | 'bin/' + hello_world_server_secure.serverKey; 180 | return hello_world_server_secure.main(); 181 | } 182 | 183 | _test() async { 184 | final client = HttpClient(); 185 | final url = Uri.https('localhost:$port', ''); 186 | final response = await client.getUrl(url); 187 | // expect(..., 'Hello, world!'); 188 | await response.close(); 189 | } 190 | 191 | expect( 192 | () => Future.any([ 193 | _server(), 194 | _test(), 195 | ]), 196 | prints(startsWith('Listening on localhost:$port'))); 197 | }, skip: 'https://github.com/dart-lang/site-www/issues/468'); 198 | } 199 | 200 | Future getUrl([ 201 | String host = 'localhost', 202 | int port = 8080, 203 | String path = '', 204 | ]) async { 205 | final client = HttpClient(); 206 | final request = await client.get(host, port, path); 207 | final response = await request.close(); 208 | final data = await utf8.decoder.bind(response).toList(); 209 | return data.join(''); 210 | } 211 | -------------------------------------------------------------------------------- /httpserver/web/make_a_guess.html: -------------------------------------------------------------------------------- 1 | 2 | 7 | 11 | 12 | 13 | 14 | 15 | 16 | Make a guess 17 | 18 | 19 | 20 |

Guess a number

21 |
22 | 34 | 35 |
36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /httpserver/web/note_client.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // Client to note_server.dart. 6 | // Use note_taker.html to run this script. 7 | 8 | import 'dart:html'; 9 | import 'dart:convert' show jsonEncode; 10 | 11 | String note; 12 | 13 | TextInputElement noteTextInput; 14 | ParagraphElement howManyNotes; 15 | TextInputElement chooseNote; 16 | ParagraphElement displayNote; 17 | HttpRequest request; 18 | String url = 'http://localhost:4042'; 19 | 20 | void main() { 21 | noteTextInput = querySelector('#note_entry') as TextInputElement; 22 | howManyNotes = querySelector('#display_how_many_notes') as ParagraphElement; 23 | chooseNote = querySelector('#choose_note') as TextInputElement; 24 | displayNote = querySelector('#display_note') as ParagraphElement; 25 | 26 | querySelector('#save_note').onClick.listen(saveNote); 27 | querySelector('#get_note').onClick.listen(requestNote); 28 | } 29 | 30 | void saveNote(Event e) { 31 | request = HttpRequest(); 32 | request.onReadyStateChange.listen(onData); 33 | 34 | request.open('POST', url); 35 | request.send(jsonEncode({'myNote': noteTextInput.value})); 36 | } 37 | 38 | void requestNote(Event e) { 39 | if (chooseNote.value.isEmpty) return; 40 | 41 | final getNoteNumber = int.tryParse(chooseNote.value) ?? 0; 42 | 43 | request = HttpRequest(); 44 | request.onReadyStateChange.listen(onData); 45 | 46 | request.open('POST', url); 47 | request.send(jsonEncode({'getNote': getNoteNumber.toString()})); 48 | } 49 | 50 | void onData(_) { 51 | if (request.readyState == HttpRequest.DONE && request.status == 200) { 52 | if (request.responseText.startsWith('You')) { 53 | howManyNotes.text = request.responseText; 54 | } else { 55 | displayNote.text = request.responseText; 56 | } 57 | } else if (request.readyState == HttpRequest.DONE && request.status == 0) { 58 | // Status is 0; most likely the server isn't running. 59 | howManyNotes.text = 'No server'; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /httpserver/web/note_taker.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | 18 | Write a Note 19 | 20 | 21 | 22 | 23 |

Write a Note

24 | 25 |
26 |
27 | 28 | 29 |


30 |
31 | 32 |
33 | 34 | 35 |


36 |
37 | 38 |
39 | 40 | 41 | -------------------------------------------------------------------------------- /its_all_about_you/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /its_all_about_you/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /its_all_about_you/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: its_all_about_you 2 | description: A sample application 3 | homepage: https://dart.dev/tutorials/web/fetch-data 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /its_all_about_you/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 12 | 13 | 14 | 15 | It's All About You 16 | 17 | 18 | 19 | 20 | 21 |

It's All About You

22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 74 | 75 | 76 | 77 | 78 |
Enter valueData typeJSON string
Favorite number:integer
Do you know pi?double
What's your sign?String
A few of your favorite things? 57 | 58 | 59 | 60 | List<String>
I love chocolate! 68 |
69 | True 70 | False 71 |
72 |
bool
79 | 80 |
81 |
82 | 83 |
84 | 85 | 86 | -------------------------------------------------------------------------------- /its_all_about_you/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015, the Dart project authors. 2 | // Please see the AUTHORS file for details. 3 | // All rights reserved. Use of this source code is governed 4 | // by a BSD-style license that can be found in the LICENSE file. 5 | 6 | import 'dart:html'; 7 | import 'dart:convert'; 8 | 9 | // Input fields 10 | InputElement favoriteNumber; 11 | InputElement valueOfPi; 12 | InputElement horoscope; 13 | InputElement favOne; 14 | InputElement favTwo; 15 | InputElement favThree; 16 | RadioButtonInputElement loveChocolate; 17 | RadioButtonInputElement noLoveForChocolate; 18 | 19 | // Result fields 20 | TextAreaElement intAsJson; 21 | TextAreaElement doubleAsJson; 22 | TextAreaElement stringAsJson; 23 | TextAreaElement listAsJson; 24 | TextAreaElement boolAsJson; 25 | TextAreaElement mapAsJson; 26 | 27 | void main() { 28 | // Set up the input text areas. 29 | favoriteNumber = querySelector('#favoriteNumber'); 30 | valueOfPi = querySelector('#valueOfPi'); 31 | horoscope = querySelector('#horoscope'); 32 | favOne = querySelector('#favOne'); 33 | favTwo = querySelector('#favTwo'); 34 | favThree = querySelector('#favThree'); 35 | loveChocolate = querySelector('#loveChocolate'); 36 | noLoveForChocolate = querySelector('#noLoveForChocolate'); 37 | 38 | // Set up the results text areas 39 | // to display the values as JSON. 40 | intAsJson = querySelector('#intAsJson'); 41 | doubleAsJson = querySelector('#doubleAsJson'); 42 | boolAsJson = querySelector('#boolAsJson'); 43 | stringAsJson = querySelector('#stringAsJson'); 44 | listAsJson = querySelector('#listAsJson'); 45 | mapAsJson = querySelector('#mapAsJson'); 46 | 47 | // Set up the listeners. 48 | favoriteNumber.onKeyUp.listen(showJson); 49 | valueOfPi.onKeyUp.listen(showJson); 50 | loveChocolate.onClick.listen(showJson); 51 | noLoveForChocolate.onClick.listen(showJson); 52 | horoscope.onKeyUp.listen(showJson); 53 | favOne.onKeyUp.listen(showJson); 54 | favTwo.onKeyUp.listen(showJson); 55 | favThree.onKeyUp.listen(showJson); 56 | 57 | _populateFromJson(); 58 | showJson(null); 59 | } 60 | 61 | // Pre-fill the form with some default values. 62 | void _populateFromJson() { 63 | final jsonDataAsString = ''' 64 | { "favoriteNumber":73, 65 | "valueOfPi":3.141592, 66 | "chocolate":true, 67 | "horoscope":"Cancer", 68 | "favoriteThings":["monkeys", 69 | "parrots", 70 | "lattes"] 71 | } 72 | '''; 73 | 74 | Map jsonData = json.decode(jsonDataAsString); 75 | 76 | favoriteNumber.value = jsonData['favoriteNumber'].toString(); 77 | valueOfPi.value = jsonData['valueOfPi'].toString(); 78 | horoscope.value = jsonData['horoscope'].toString(); 79 | favOne.value = jsonData['favoriteThings'][0]; 80 | favTwo.value = jsonData['favoriteThings'][1]; 81 | favThree.value = jsonData['favoriteThings'][2]; 82 | 83 | if (jsonData['chocolate']) { 84 | loveChocolate.checked = true; 85 | } else { 86 | noLoveForChocolate.checked = true; 87 | } 88 | } 89 | 90 | // Display all values as JSON. 91 | void showJson(Event e) { 92 | // Grab the data that will be converted to JSON. 93 | final favNum = int.parse(favoriteNumber.value); 94 | final pi = double.parse(valueOfPi.value); 95 | final chocolate = loveChocolate.checked; 96 | final sign = horoscope.value; 97 | final favoriteThings = [favOne.value, favTwo.value, favThree.value]; 98 | 99 | final formData = { 100 | 'favoriteNumber': favNum, 101 | 'valueOfPi': pi, 102 | 'chocolate': chocolate, 103 | 'horoscope': sign, 104 | 'favoriteThings': favoriteThings 105 | }; 106 | 107 | // Convert everything to JSON and 108 | // display the results. 109 | intAsJson.text = json.encode(favNum); 110 | doubleAsJson.text = json.encode(pi); 111 | boolAsJson.text = json.encode(chocolate); 112 | stringAsJson.text = json.encode(sign); 113 | listAsJson.text = json.encode(favoriteThings); 114 | mapAsJson.text = json.encode(formData); 115 | } 116 | -------------------------------------------------------------------------------- /its_all_about_you/web/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #F8F8F8; 3 | font-family: 'Open Sans', sans-serif; 4 | font-size: 14px; 5 | font-weight: normal; 6 | line-height: 1.2em; 7 | margin: 15px; 8 | } 9 | 10 | h1, p, td, th, label, table { 11 | color: #333; 12 | } 13 | 14 | table { 15 | text-align: left; 16 | border-spacing: 5px 15px 17 | } 18 | 19 | label { 20 | font-weight: bold; 21 | } 22 | 23 | textarea { 24 | resize: none; 25 | } 26 | 27 | .result { 28 | background-color: Ivory; 29 | padding: 5px 5px 5px 5px; 30 | border: 1px solid black; 31 | } 32 | 33 | #mapAsJson { 34 | background-color: Ivory; 35 | padding: 5px 5px 5px 5px; 36 | margin-top: 15px; 37 | border: 1px solid black; 38 | width: 500px; 39 | height: 50px; 40 | font-size:14px; 41 | } 42 | 43 | table { 44 | text-align: left; 45 | border-spacing: 5px 15px 46 | } 47 | 48 | label { 49 | font-weight: bold; 50 | } 51 | 52 | textarea { 53 | resize: none; 54 | } 55 | -------------------------------------------------------------------------------- /mini/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /mini/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /mini/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: mini 2 | description: A sample application 3 | homepage: https://dart.dev/tutorials/web/low-level-html/connect-dart-html 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /mini/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 12 | 13 | 14 | 15 | A Minimalist App 16 | 17 | 18 | 19 | 20 |

21 | RipVanWinkle paragraph. 22 |

23 | 24 | 25 | -------------------------------------------------------------------------------- /mini/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'dart:html'; 6 | 7 | void main() { 8 | querySelector('#RipVanWinkle').text = 'Wake up, sleepy head!'; 9 | } 10 | -------------------------------------------------------------------------------- /mini_with_style/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /mini_with_style/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /mini_with_style/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: mini_with_style 2 | description: A sample application 3 | homepage: https://dart.dev/tutorials/web/low-level-html/connect-dart-html 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /mini_with_style/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 12 | 13 | 14 | 15 | A Minimalist App 16 | 17 | 18 | 19 | 20 | 21 |

22 | RipVanWinkle paragraph. 23 |

24 | 25 | 26 | -------------------------------------------------------------------------------- /mini_with_style/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'dart:html'; 6 | 7 | void main() { 8 | querySelector('#RipVanWinkle').text = 'Wake up, sleepy head!'; 9 | } 10 | -------------------------------------------------------------------------------- /mini_with_style/web/styles.css: -------------------------------------------------------------------------------- 1 | #RipVanWinkle { 2 | font-size: 20px; 3 | font-family: 'Open Sans', sans-serif; 4 | text-align: center; 5 | margin-top: 20px; 6 | background-color: SlateBlue; 7 | color: Yellow; 8 | } 9 | -------------------------------------------------------------------------------- /portmanteaux/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /portmanteaux/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /portmanteaux/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: portmanteaux 2 | description: A sample application 3 | homepage: https://dart.dev/tutorials/web/fetch-data 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /portmanteaux/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 12 | 13 | 14 | 15 | Portmanteaux 16 | 17 | 18 | 19 | 20 | 21 |

Portmanteaux

22 | 23 | 24 | 25 |
    26 |
27 | 28 | 29 | -------------------------------------------------------------------------------- /portmanteaux/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012, the Dart project authors. 2 | // Please see the AUTHORS file for details. 3 | // All rights reserved. Use of this source code 4 | // is governed by a BSD-style license that can be 5 | // found in the LICENSE file. 6 | 7 | import 'dart:html'; 8 | import 'dart:convert'; 9 | 10 | UListElement wordList; 11 | 12 | void main() { 13 | querySelector('#getWords').onClick.listen(makeRequest); 14 | wordList = querySelector('#wordList') as UListElement; 15 | } 16 | 17 | void makeRequest(Event e) { 18 | var path = 'https://dart.dev/f/portmanteaux.json'; 19 | var httpRequest = HttpRequest(); 20 | httpRequest 21 | ..open('GET', path) 22 | ..onLoadEnd.listen((e) => requestComplete(httpRequest)) 23 | ..send(''); 24 | } 25 | 26 | void requestComplete(HttpRequest request) { 27 | if (request.status == 200) { 28 | final portmanteaux = 29 | (json.decode(request.responseText) as List).cast(); 30 | for (var i = 0; i < portmanteaux.length; i++) { 31 | wordList.children.add(LIElement()..text = portmanteaux[i]); 32 | } 33 | } else { 34 | wordList.children 35 | .add(LIElement()..text = 'Request failed, status=${request.status}'); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /portmanteaux/web/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #F8F8F8; 3 | font-family: 'Open Sans', sans-serif; 4 | font-size: 14px; 5 | font-weight: normal; 6 | line-height: 1.2em; 7 | margin: 15px; 8 | } 9 | 10 | h1, p, li { 11 | color: #333; 12 | } 13 | -------------------------------------------------------------------------------- /portmanteaux_simple/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /portmanteaux_simple/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /portmanteaux_simple/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: portmanteaux_simple 2 | description: A sample application 3 | homepage: https://dart.dev/tutorials/web/fetch-data 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /portmanteaux_simple/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 12 | 13 | 14 | 15 | Portmanteaux 16 | 17 | 18 | 19 | 20 | 21 |

Portmanteaux

22 | 23 | 24 | 25 |
    26 |
27 | 28 | 29 | -------------------------------------------------------------------------------- /portmanteaux_simple/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012, the Dart project authors. 2 | // Please see the AUTHORS file for details. 3 | // All rights reserved. Use of this source code 4 | // is governed by a BSD-style license that can be 5 | // found in the LICENSE file. 6 | 7 | import 'dart:async'; 8 | import 'dart:convert'; 9 | import 'dart:html'; 10 | 11 | UListElement wordList; 12 | 13 | void main() { 14 | querySelector('#getWords').onClick.listen(makeRequest); 15 | wordList = querySelector('#wordList') as UListElement; 16 | } 17 | 18 | Future makeRequest(Event e) async { 19 | var path = 'https://dart.dev/f/portmanteaux.json'; 20 | try { 21 | processString(await HttpRequest.getString(path)); 22 | } catch (e) { 23 | print('Couldn\'t open $path: $e'); 24 | handleError(e); 25 | } 26 | } 27 | 28 | void processString(String jsonString) { 29 | final portmanteaux = 30 | (json.decode(jsonString) as List).cast(); 31 | for (var i = 0; i < portmanteaux.length; i++) { 32 | wordList.children.add(LIElement()..text = portmanteaux[i]); 33 | } 34 | } 35 | 36 | void handleError(Object error) { 37 | wordList.children.add(LIElement()..text = 'Request failed.'); 38 | } 39 | -------------------------------------------------------------------------------- /portmanteaux_simple/web/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #F8F8F8; 3 | font-family: 'Open Sans', sans-serif; 4 | font-size: 14px; 5 | font-weight: normal; 6 | line-height: 1.2em; 7 | margin: 15px; 8 | } 9 | 10 | h1, p, li { 11 | color: #333; 12 | } 13 | 14 | #sample_container_id { 15 | width: 100%; 16 | height: 400px; 17 | position: relative; 18 | border: 1px solid #ccc; 19 | background-color: #fff; 20 | } 21 | -------------------------------------------------------------------------------- /runtests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | shopt -s nullglob 4 | 5 | EXITSTATUS=0 6 | PASSING=0 7 | WARNINGS=0 8 | FAILURES=0 9 | 10 | ##### 11 | # Type Analysis 12 | 13 | #ANA="dart_analyzer --fatal-type-errors --extended-exit-code --type-checks-for-inferred-types" 14 | # use new experimental analyzer 15 | ANA="dartanalyzer --fatal-type-errors" 16 | 17 | echo 18 | echo "Type Analysis, running dart_analyzer..." 19 | 20 | pub_result=`pub install` 21 | cmd="$ANA --package-root packages" 22 | 23 | for dir in web/* bin/* 24 | do 25 | echo $dir 26 | # Run pub if there is a pubspec in this code directory. 27 | # if [ -a "$dir/pubspec.yaml" ]; then 28 | # pub_result=`pushd $dir && pub install && popd` 29 | # cmd="$ANA --package-root $dir/packages" 30 | # else 31 | # cmd="$ANA" 32 | # fi 33 | 34 | # Loop through each Dart file in this code directory. 35 | files="$dir/*.dart" 36 | for file in $files 37 | do 38 | results=`$cmd $file 2>&1` 39 | exit_code=$? 40 | if [ $exit_code -eq 2 ]; then 41 | let FAILURES++ 42 | EXITSTATUS=1 43 | echo "$results" 44 | echo "$file: FAILURE." 45 | elif [ $exit_code -eq 1 ]; then 46 | let WARNINGS++ 47 | echo "$results" 48 | echo "$file: WARNING." 49 | elif [ $exit_code -eq 0 ]; then 50 | let PASSING++ 51 | echo "$results" 52 | echo "$file: Passed analysis." 53 | else 54 | echo "$file: Unknown exit code: $exit_code." 55 | fi 56 | # Remove the output directory so that subsequent test runs will still see 57 | # the warnings and errors. 58 | rm -rf out/ 59 | done 60 | done 61 | 62 | echo 63 | echo "####################################################" 64 | echo "PASSING = $PASSING" 65 | echo "WARNINGS = $WARNINGS" 66 | echo "FAILURES = $FAILURES" 67 | echo "####################################################" 68 | echo 69 | exit $EXITSTATUS 70 | -------------------------------------------------------------------------------- /search_form/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /search_form/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /search_form/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: search_form 2 | description: A sample application 3 | 4 | environment: 5 | sdk: '>=2.3.0 <3.0.0' 6 | 7 | dev_dependencies: 8 | build_runner: ^1.5.0 9 | build_web_compilers: ^2.1.0 10 | pedantic: ^1.7.0 11 | -------------------------------------------------------------------------------- /search_form/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | 11 | 12 | 13 | Search for it 14 | 15 | 16 | 17 | 18 |

Search dart.dev

19 | 20 |
21 |
24 | 26 | 27 | 32 |
33 |
34 | 35 | 36 | -------------------------------------------------------------------------------- /search_form/web/readme: -------------------------------------------------------------------------------- 1 | This is a fake readme file to thwart generation of the gist files. 2 | Please ignore. 3 | -------------------------------------------------------------------------------- /search_form/web/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #F8F8F8; 3 | font-family: 'Open Sans', sans-serif; 4 | font-size: 14px; 5 | font-weight: normal; 6 | line-height: 1.2em; 7 | margin: 15px; 8 | } 9 | 10 | h1, p { 11 | color: #333; 12 | } 13 | 14 | #sample_container_id { 15 | width: 100%; 16 | height: 400px; 17 | position: relative; 18 | border: 1px solid #ccc; 19 | background-color: #fff; 20 | } 21 | -------------------------------------------------------------------------------- /streams/last_positive/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /streams/last_positive/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /streams/last_positive/bin/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015, the Dart project authors. 2 | // Please see the AUTHORS file for details. 3 | // All rights reserved. Use of this source code is governed 4 | // by a BSD-style license that can be found in the LICENSE file. 5 | 6 | import 'dart:async'; 7 | 8 | Future lastPositive(Stream stream) async { 9 | var lastValue; 10 | await for (var value in stream) { 11 | if (value < 0) continue; 12 | lastValue = value; 13 | } 14 | return lastValue; 15 | } 16 | 17 | Future main() async { 18 | var data = [1, -2, 3, -4, 5, -6, 7, -8, 9, -10]; 19 | var stream = Stream.fromIterable(data); 20 | var lastPos = await lastPositive(stream); 21 | print(lastPos); // 9 22 | } 23 | -------------------------------------------------------------------------------- /streams/last_positive/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: last_positive 2 | description: A simple app that reads from an integer stream and reports the last positive value from the stream. 3 | homepage: https://dart.dev/tutorials/language/streams 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | pedantic: ^1.7.0 10 | test: ^1.6.0 11 | -------------------------------------------------------------------------------- /streams/read_file/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /streams/read_file/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /streams/read_file/bin/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015, the Dart project authors. 2 | // Please see the AUTHORS file for details. 3 | // All rights reserved. Use of this source code is governed 4 | // by a BSD-style license that can be found in the LICENSE file. 5 | 6 | import 'dart:async'; 7 | import 'dart:convert'; 8 | import 'dart:io'; 9 | 10 | Future main(List args) async { 11 | if (args.isEmpty) { 12 | print("Requires a filepath as the first argument"); 13 | return; 14 | } 15 | 16 | var file = File(args[0]); 17 | var lines = 18 | utf8.decoder.bind(file.openRead()).transform(const LineSplitter()); 19 | await for (var line in lines) { 20 | if (!line.startsWith('#')) { 21 | print(line); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /streams/read_file/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: read_file 2 | description: A simple app that reads a file (specified on the command line), decodes, and prints it out. 3 | homepage: https://dart.dev/tutorials/language/streams 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | pedantic: ^1.7.0 10 | test: ^1.6.0 11 | -------------------------------------------------------------------------------- /streams/simple_stream/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /streams/simple_stream/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /streams/simple_stream/bin/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015, the Dart project authors. 2 | // Please see the AUTHORS file for details. 3 | // All rights reserved. Use of this source code is governed 4 | // by a BSD-style license that can be found in the LICENSE file. 5 | 6 | import 'dart:async'; 7 | 8 | Future sumStream(Stream stream) async { 9 | var sum = 0; 10 | await for (var value in stream) { 11 | sum += value; 12 | } 13 | return sum; 14 | } 15 | 16 | Stream countStream(int to) async* { 17 | for (var i = 1; i <= to; i++) { 18 | yield i; 19 | } 20 | } 21 | 22 | Future main() async { 23 | var stream = countStream(10); 24 | var sum = await sumStream(stream); 25 | print(sum); // 55 26 | } 27 | -------------------------------------------------------------------------------- /streams/simple_stream/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: simple_stream 2 | description: A simple application that reads integers from a stream and adds their values together. 3 | homepage: https://dart.dev/tutorials/language/streams 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | pedantic: ^1.7.0 10 | test: ^1.6.0 11 | -------------------------------------------------------------------------------- /streams/throw_error/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /streams/throw_error/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /streams/throw_error/bin/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015, the Dart project authors. 2 | // Please see the AUTHORS file for details. 3 | // All rights reserved. Use of this source code is governed 4 | // by a BSD-style license that can be found in the LICENSE file. 5 | 6 | import 'dart:async'; 7 | 8 | Future sumStream(Stream stream) async { 9 | var sum = 0; 10 | try { 11 | await for (var value in stream) { 12 | sum += value; 13 | } 14 | } catch (error) { 15 | return -1; 16 | } 17 | return sum; 18 | } 19 | 20 | Stream countStream(int to) async* { 21 | for (var i = 1; i <= to; i++) { 22 | if (i == 4) { 23 | // ignore: only_throw_errors 24 | throw "Whoops!"; // Intentional error 25 | } else { 26 | yield i; 27 | } 28 | } 29 | } 30 | 31 | Future main() async { 32 | var stream = countStream(10); 33 | var sum = await sumStream(stream); 34 | print(sum); // -1 35 | } 36 | -------------------------------------------------------------------------------- /streams/throw_error/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: throw_error 2 | description: A simple app that reads from a stream and throws an error when the loop iterator equals 4. 3 | homepage: https://dart.dev/tutorials/language/streams 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | pedantic: ^1.7.0 10 | test: ^1.6.0 11 | -------------------------------------------------------------------------------- /todo/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /todo/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /todo/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: todo 2 | description: A sample application 3 | homepage: https://dart.dev/tutorials/web/low-level-html/add-elements 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /todo/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 12 | 13 | 14 | 15 | Todo 16 | 17 | 18 | 19 | 20 | 21 |

Procrastinator's Todo

22 | 23 |
24 | 25 |
26 | 27 |
28 |
    29 |
30 |
31 | 32 | 33 | -------------------------------------------------------------------------------- /todo/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012, the Dart project authors. 2 | // Please see the AUTHORS file for details. 3 | // All rights reserved. Use of this source code 4 | // is governed by a BSD-style license that can be 5 | // found in the LICENSE file. 6 | 7 | import 'dart:html'; 8 | 9 | InputElement toDoInput; 10 | UListElement toDoList; 11 | 12 | void main() { 13 | toDoInput = querySelector('#to-do-input'); 14 | toDoList = querySelector('#to-do-list'); 15 | toDoInput.onChange.listen(addToDoItem); 16 | } 17 | 18 | void addToDoItem(Event e) { 19 | var newToDo = LIElement(); 20 | newToDo.text = toDoInput.value; 21 | toDoInput.value = ''; 22 | toDoList.children.add(newToDo); 23 | } 24 | -------------------------------------------------------------------------------- /todo/web/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: 'Open Sans', sans-serif; 3 | background-color: WhiteSmoke; 4 | margin: 15px; 5 | color: black; 6 | } 7 | 8 | #to-do-input { 9 | font-family: 'Open Sans', sans-serif; 10 | font-size: 14px; 11 | font-weight: normal; 12 | padding: 5px 0px 5px 5px; 13 | width: 100%; 14 | border: 1px solid Silver; 15 | background-color: White; 16 | } 17 | 18 | #to-do-list { 19 | padding:0px; 20 | margin:0px; 21 | list-style-position:inside; 22 | } 23 | 24 | #to-do-list li { 25 | padding:5px 0px 5px 5px; 26 | border-bottom: 1px dotted Silver; 27 | } 28 | -------------------------------------------------------------------------------- /todo_with_delete/.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build outputs 6 | build/ 7 | 8 | # Directory created by dartdoc 9 | doc/api/ 10 | -------------------------------------------------------------------------------- /todo_with_delete/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | linter: 7 | rules: 8 | - omit_local_variable_types 9 | -------------------------------------------------------------------------------- /todo_with_delete/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: todo_with_delete 2 | description: A sample application 3 | homepage: https://dart.dev/tutorials/web/low-level-html/remove-elements 4 | 5 | environment: 6 | sdk: '>=2.3.0 <3.0.0' 7 | 8 | dev_dependencies: 9 | build_runner: ^1.5.0 10 | build_web_compilers: ^2.1.0 11 | pedantic: ^1.7.0 12 | -------------------------------------------------------------------------------- /todo_with_delete/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 12 | 13 | 14 | 15 | Todo 16 | 17 | 18 | 19 | 20 | 21 |

Todo

22 | 23 |
24 | 25 |
26 | 27 |
28 |
    29 |
30 |
31 | 32 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /todo_with_delete/web/main.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012, the Dart project authors. 2 | // Please see the AUTHORS file // for details. 3 | // All rights reserved. Use of this source code 4 | // is governed by a BSD-style license that can be 5 | // found in the LICENSE file. 6 | 7 | import 'dart:html'; 8 | 9 | InputElement toDoInput; 10 | UListElement toDoList; 11 | ButtonElement deleteAll; 12 | 13 | void main() { 14 | toDoInput = querySelector('#to-do-input'); 15 | toDoList = querySelector('#to-do-list'); 16 | toDoInput.onChange.listen(addToDoItem); 17 | deleteAll = querySelector('#delete-all'); 18 | deleteAll.onClick.listen((e) => toDoList.children.clear()); 19 | } 20 | 21 | void addToDoItem(Event e) { 22 | var newToDo = LIElement(); 23 | newToDo.text = toDoInput.value; 24 | newToDo.onClick.listen((e) => newToDo.remove()); 25 | toDoInput.value = ''; 26 | toDoList.children.add(newToDo); 27 | } 28 | -------------------------------------------------------------------------------- /todo_with_delete/web/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: 'Open Sans', sans-serif; 3 | background-color: WhiteSmoke; 4 | margin: 15px; 5 | color: black; 6 | } 7 | 8 | #to-do-input { 9 | font-family: 'Open Sans', sans-serif; 10 | font-size: 14px; 11 | font-weight: normal; 12 | padding: 5px 0px 5px 5px; 13 | width: 100%; 14 | border: 1px solid Silver; 15 | background-color: White; 16 | } 17 | 18 | #to-do-list { 19 | padding: 0; 20 | margin: 0; 21 | list-style-position: inside; 22 | } 23 | 24 | #to-do-list li { 25 | padding: 5px 0px 5px 5px; 26 | border-bottom: 1px dotted Silver; 27 | } 28 | 29 | #to-do-list li:hover { 30 | color: red; 31 | cursor: pointer; 32 | } 33 | 34 | #delete-all { 35 | margin-top: 8px; 36 | background-color: #F8F8F8; 37 | border: 1px dotted #ccc; 38 | border-radius: 1em; 39 | float: right; 40 | } 41 | 42 | #delete-all:hover { 43 | background-color: #ddd; 44 | } 45 | --------------------------------------------------------------------------------