| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 // Demonstrates: | 6 // Demonstrates: |
| 7 // list, maps, random, strings, string interpolation | 7 // list, maps, random, strings, string interpolation |
| 8 // cascade, fat arrow, ternary operator | 8 // cascade, fat arrow, ternary operator |
| 9 // named constructors | 9 // named constructors |
| 10 // optional parameters | 10 // optional parameters |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 | 77 |
| 78 PirateName getBadgeNameFromStorage() { | 78 PirateName getBadgeNameFromStorage() { |
| 79 String storedName = window.localStorage[TREASURE_KEY]; | 79 String storedName = window.localStorage[TREASURE_KEY]; |
| 80 if (storedName != null) { | 80 if (storedName != null) { |
| 81 return new PirateName.fromJSON(storedName); | 81 return new PirateName.fromJSON(storedName); |
| 82 } else { | 82 } else { |
| 83 return null; | 83 return null; |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 /* | |
| 88 * A class declaration. | |
| 89 */ | |
| 90 class PirateName { | 87 class PirateName { |
| 91 | 88 |
| 92 // static variables are shared by all instances. | |
| 93 // Random is a random number generator in dart:math. | |
| 94 static final Random indexGen = new Random(); | 89 static final Random indexGen = new Random(); |
| 95 | 90 |
| 96 // List is a parameterized type. You can declare the type of the objects it co
ntains. | |
| 97 static List<String> names = []; | 91 static List<String> names = []; |
| 98 static List<String> appellations = []; | 92 static List<String> appellations = []; |
| 99 | 93 |
| 100 // Instance variables. Private variables have names that start with underscore
'_'. | |
| 101 String _firstName; | 94 String _firstName; |
| 102 String _appellation; | 95 String _appellation; |
| 103 | 96 |
| 104 // A constructor with two optional, named parameters. | |
| 105 PirateName({String firstName, String appellation}) { | 97 PirateName({String firstName, String appellation}) { |
| 106 | 98 |
| 107 // Use nextInt to get a random integer from a Random object. | |
| 108 // Use length to get the number of items in a list. | |
| 109 if (firstName == null) { | 99 if (firstName == null) { |
| 110 _firstName = names[indexGen.nextInt(names.length)]; | 100 _firstName = names[indexGen.nextInt(names.length)]; |
| 111 } else { | 101 } else { |
| 112 _firstName = firstName; | 102 _firstName = firstName; |
| 113 } | 103 } |
| 114 if (appellation == null) { | 104 if (appellation == null) { |
| 115 _appellation = appellations[indexGen.nextInt(appellations.length)]; | 105 _appellation = appellations[indexGen.nextInt(appellations.length)]; |
| 116 } else { | 106 } else { |
| 117 _appellation = appellation; | 107 _appellation = appellation; |
| 118 } | 108 } |
| 119 } | 109 } |
| 120 | 110 |
| 121 // A named constructor. | |
| 122 PirateName.fromJSON(String jsonString) { | 111 PirateName.fromJSON(String jsonString) { |
| 123 // JSON is the default implementation of a JSON encoder/decoder. | |
| 124 // Map is a collection of key-value pairs. | |
| 125 Map storedName = JSON.decode(jsonString); | 112 Map storedName = JSON.decode(jsonString); |
| 126 _firstName = storedName['f']; | 113 _firstName = storedName['f']; |
| 127 _appellation = storedName['a']; | 114 _appellation = storedName['a']; |
| 128 } | 115 } |
| 129 | 116 |
| 130 // Fat arrow syntax is shorthand for a one-line function that returns a value. | |
| 131 String toString() => pirateName; | 117 String toString() => pirateName; |
| 132 | 118 |
| 133 // A getter provides read access to the member of an object. | 119 String get jsonString => JSON.encode({"f": _firstName, "a": _appellation}); |
| 134 String get jsonString => '{ "f": "$_firstName", "a": "$_appellation" } '; | |
| 135 | 120 |
| 136 // The ternary operator is shorthand for if-then-else. | |
| 137 // String interpolation lets you easily build strings from other objects. | |
| 138 String get pirateName => _firstName.isEmpty ? '' : '$_firstName the $_appellat
ion'; | 121 String get pirateName => _firstName.isEmpty ? '' : '$_firstName the $_appellat
ion'; |
| 139 | 122 |
| 140 static Future readyThePirates() { | 123 static Future readyThePirates() { |
| 141 String path = 'piratenames.json'; | 124 String path = 'piratenames.json'; |
| 142 return HttpRequest.getString(path) | 125 return HttpRequest.getString(path) |
| 143 .then(_parsePirateNamesFromJSON); | 126 .then(_parsePirateNamesFromJSON); |
| 144 } | 127 } |
| 145 | 128 |
| 146 static _parsePirateNamesFromJSON(String jsonString) { | 129 static _parsePirateNamesFromJSON(String jsonString) { |
| 147 Map pirateNames = JSON.decode(jsonString); | 130 Map pirateNames = JSON.decode(jsonString); |
| 148 names = pirateNames['names']; | 131 names = pirateNames['names']; |
| 149 appellations = pirateNames['appellations']; | 132 appellations = pirateNames['appellations']; |
| 150 } | 133 } |
| 151 } | 134 } |
| OLD | NEW |