OLD | NEW |
1 --- | 1 --- |
2 layout: homepage | 2 layout: homepage |
3 googleapi: true | 3 googleapi: true |
4 js: | 4 js: |
5 - url: /js/display-news.js | 5 - url: /js/display-news.js |
6 - url: /js/os-switcher.js | 6 - url: /js/os-switcher.js |
7 defer: true | 7 defer: true |
8 - url: /js/editor-downloads-analytics.js | 8 - url: /js/editor-downloads-analytics.js |
9 defer: true | 9 defer: true |
10 - url: /js/editor-version.js | 10 - url: /js/editor-version.js |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 <div id="feed-posts"> | 133 <div id="feed-posts"> |
134 </div> | 134 </div> |
135 </div> | 135 </div> |
136 </div> | 136 </div> |
137 </div> | 137 </div> |
138 </section> | 138 </section> |
139 | 139 |
140 <section id="code"> | 140 <section id="code"> |
141 <h1> Code </h1> | 141 <h1> Code </h1> |
142 | 142 |
143 <p> | 143 <p> |
144 Experiment with Dart right in your browser: | 144 You can find code examples in Dart Editor's welcome page, |
145 <a href="http://try.dartlang.org">try.dartlang.org</a> | 145 in the <a href="/samples/">Samples page</a>, |
| 146 and below. |
| 147 You can also experiment with Dart right in your browser: |
| 148 <a href="http://try.dartlang.org" target="_blank">try.dartlang.org</a> |
146 </p> | 149 </p> |
147 | 150 |
148 <p> | 151 <div class="tabbable"> |
149 Here's some code from a Dart web app that uses Web UI. | 152 <ul class="nav nav-tabs"> |
150 To learn about web programming with Dart, try our | 153 <li class="active"><a href="#language" data-toggle="tab">Syntax</a></li> |
151 <a href="/docs/tutorials/">Dart tutorials</a>. | 154 <li><a href="#types" data-toggle="tab">Optional types</a><
/li> |
152 </p> | 155 <li><a href="#futures" data-toggle="tab">Futures</a></li> |
| 156 <li><a href="#webui" data-toggle="tab">Web UI</a></li> |
| 157 </ul> |
153 | 158 |
154 <div class="row-fluid"> | 159 <div class="tab-content"> |
155 <div class="span6" id="dart-code"> | 160 <div class="tab-pane active" id="language"> |
| 161 <p> |
| 162 Dart aims to be unsurprising, |
| 163 but it does add a few language features |
| 164 to keep your code succinct yet understandable. |
| 165 Read all about Dart syntax in the |
| 166 <a href="/docs/dart-up-and-running/contents/ch02.html">Dart language tou
r</a>. |
| 167 </p> |
| 168 |
| 169 <div class="row-fluid"> |
| 170 <div class="span10 offset1"> |
| 171 {% prettify dart %} |
| 172 [[highlight]]import[[/highlight]] 'dart:html'; // Dart supp
orts libraries. |
| 173 |
| 174 // Define functions either inside or outside of classes. |
| 175 startOrEndTest() { |
| 176 // ... |
| 177 } |
| 178 |
| 179 [[highlight]]class[[/highlight]] Rectangle implements Shape { // Declaring a cl
ass... |
| 180 final num height, width; // ...with properties. |
| 181 num [[highlight]]get[[/highlight]] area [[highlight]]=>[[/highlight]] height *
width; // A property implemented with a getter. |
| 182 // Also: function shorthand syntax (=>). |
| 183 Rectangle([[highlight]]this.height, this.width[[/highlight]]); // Compact cons
tructor syntax. |
| 184 } |
| 185 |
| 186 // Every app has a main() function, where execution starts. |
| 187 [[highlight]]main()[[/highlight]] { |
| 188 // The cascade operator (..) saves you from repetitive typing. |
| 189 query("#button") |
| 190 [[highlight]]..[[/highlight]]text = "Run test" |
| 191 [[highlight]]..[[/highlight]]onClick.listen(startOrEndTest); |
| 192 } |
| 193 {% endprettify %} |
| 194 </div> |
| 195 </div> |
| 196 </div> |
| 197 |
| 198 <div class="tab-pane" id="types"> |
| 199 <p> |
| 200 Declaring types is optional in Dart. |
| 201 These types don't affect the runtime behavior, |
| 202 but you can use them to clarify APIs |
| 203 and get better help from tools—for example, |
| 204 better code completion, easier navigation, and earlier error detection. |
| 205 For details, see |
| 206 <a href="/articles/optional-types/">Optional Types in Dart</a>. |
| 207 </p> |
| 208 |
| 209 <div class="row-fluid"> |
| 210 <div class="span6" id="without-types"> |
| 211 {% prettify dart %} |
| 212 // Without types. |
| 213 recalculate(origin, offset, estimate) { |
| 214 // ... |
| 215 } |
| 216 {% endprettify %} |
| 217 </div> |
| 218 <div class="span6" id="with-types"> |
| 219 {% prettify dart %} |
| 220 // With types (and an optional parameter). |
| 221 [[highlight]]num[[/highlight]] recalculate([[highlight]]Point[[/highlight]] orig
in, |
| 222 [[highlight]]num[[/highlight]] offset, |
| 223 {[[highlight]]bool[[/highlight]] estimate:false}) { |
| 224 // ... |
| 225 } |
| 226 {% endprettify %} |
| 227 </div> |
| 228 </div> |
| 229 </div> |
| 230 |
| 231 <div class="tab-pane" id="futures"> |
| 232 <p> |
| 233 Functions that do potentially expensive work return Futures. |
| 234 To specify what happens when the function's work is done, |
| 235 invoke <code>then()</code> on the Future. |
| 236 To handle errors, invoke <code>catchError()</code>. |
| 237 Learn more by reading |
| 238 <a href="/articles/using-future-based-apis/">Using Future Based APIs</a>
. |
| 239 </p> |
| 240 |
| 241 <div class="row-fluid"> |
| 242 <div class="span10 offset1"> |
| 243 |
| 244 {% prettify dart %} |
| 245 import 'dart:io'; |
| 246 import 'dart:async'; |
| 247 |
| 248 void printDailyNewsDigest() { |
| 249 File file = new File("dailyNewsDigest.txt"); |
| 250 [[highlight]]file.readAsString()[[/highlight]] // readAsStrin
g() returns a Future. |
| 251 [[highlight]].then[[/highlight]]((content) => print(content)) // Handle succ
essful completion. |
| 252 [[highlight]].catchError[[/highlight]]((error) => // Handle fail
ure. |
| 253 print("Sorry, no news today. Here's why:\n$error")); |
| 254 } |
| 255 {% endprettify %} |
| 256 </div> |
| 257 </div> |
| 258 </div> |
| 259 <div class="tab-pane" id="webui"> |
| 260 <p> |
| 261 Here's an example of implementing a web app using the Web UI package. |
| 262 To learn about web programming with Dart, try our |
| 263 <a href="/docs/tutorials/">Dart tutorials</a>. |
| 264 </p> |
| 265 |
| 266 <div class="row-fluid"> |
| 267 <div class="span6" id="dart-code"> |
156 {% prettify dart %} | 268 {% prettify dart %} |
157 // Dart code: | 269 // Dart code: |
158 import 'package:web_ui/web_ui.dart'; | 270 import 'package:web_ui/web_ui.dart'; |
159 | 271 |
160 @observable String myString = ''; | 272 @observable |
| 273 var [[highlight]]shout[[/highlight]] = new Shout(); |
161 | 274 |
162 String get shouted => myString.toUpperCase(); | 275 @observable |
163 | 276 class Shout { |
164 String get palindrome => | 277 String [[highlight]]myString[[/highlight]] = ''; |
165 myString + myString.split('').reversed.join(); | 278 String get [[highlight]]shouted[[/highlight]] => myString.toUpperCase(); |
| 279 String get [[highlight]]palindrome[[/highlight]] => |
| 280 myString + myString.split('').reversed.join(); |
| 281 } |
166 {% endprettify %} | 282 {% endprettify %} |
167 </div> | 283 </div> |
168 <div class="span6" id="html-code"> | 284 <div class="span6" id="html-code"> |
169 {% prettify html %} | 285 {% prettify html %} |
170 <!-- HTML code: --> | 286 <!-- HTML code: --> |
171 <input type="text" bind-value="myString" | 287 <template id="stringManipulation" |
172 placeholder="Type here"> | 288 instantiate="if [[highlight]]shout[[/highlight]] != null"> |
173 | 289 <input type="text" |
174 <div> Shouted: {{'{{'}}shouted}} </div> | 290 bind-value="shout.[[highlight]]myString[[/highlight]]" |
175 <div> Palindromic: {{'{{'}}palindrome}} </div> | 291 placeholder="Type here"> |
176 | 292 |
177 <script type="application/dart" src="little.dart"></script> | 293 <div> Shouted: {{'{{'}}shout.[[highlight]]shouted[[/highlight]]}} </div> |
178 <script src="packages/browser/dart.js"></script> | 294 <div> Palindromic: {{'{{'}}shout.[[highlight]]palindrome[[/highlight]]}} </div
> |
| 295 </template> |
179 {% endprettify %} | 296 {% endprettify %} |
180 {% comment %} | 297 {% comment %} |
181 Beginning double curlies are processed by liquid, | 298 Beginning double curlies are processed by liquid, |
182 so you have to surround them in single quotes, | 299 so you have to surround them in single quotes, |
183 surrounded by double curlies. Yup. | 300 surrounded by double curlies. Yup. |
184 {% endcomment %} | 301 {% endcomment %} |
185 </div> | 302 </div> |
186 </div> <!-- code --> | 303 </div> <!-- code --> |
187 | 304 </div> |
| 305 </div> |
| 306 </div> |
188 </section> | 307 </section> |
189 | 308 |
190 | 309 |
191 <section id="get-started"> | 310 <section id="get-started"> |
192 | 311 |
193 <h1> Get started </h1> | 312 <h1> Get started </h1> |
194 | 313 |
195 <p> | 314 <p> |
196 Getting started with Dart is easy. | 315 Getting started with Dart is easy. |
197 Just download Dart, | 316 Just download Dart, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 You can write Dart apps using any recent version of | 387 You can write Dart apps using any recent version of |
269 {% include os-choices.html %} | 388 {% include os-choices.html %} |
270 The Dart development tools <b>do not support Windows XP</b>. | 389 The Dart development tools <b>do not support Windows XP</b>. |
271 Dart Editor requires Java version 6 or higher. | 390 Dart Editor requires Java version 6 or higher. |
272 </aside> | 391 </aside> |
273 | 392 |
274 </section> | 393 </section> |
275 | 394 |
276 </article> <!-- /.homepage --> | 395 </article> <!-- /.homepage --> |
277 | 396 |
OLD | NEW |