【发布时间】:2014-09-29 19:33:54
【问题描述】:
我正在阅读the "Avast, Ye Pirates" example,它非常有趣。但是,作为一名大学生(没有编程经验),要掌握 RETURNING a Future 的概念还是有些困难。
有什么区别
这个,
void doStuff() {
someAsyncProcess().then((msg) => msg.result);
}
还有这个
Future doStuff() {
return someAsyncProcess().then((msg) => msg.result);
}
另一个问题:什么是(_)参数?为什么不只是一个空白 ( )。
阅读 Pirate 示例后,我重新排列或重写了它的一些代码,将代码行数减少了三分之一或更少。虽然重新排列示例代码比阅读它更有趣,但我的代码变得越来越混乱,我的大脑也是如此。是否有关于如何在文件中排列类和函数等的 Dart 指南?也就是说,如何构造一个程序和一个文件的全部代码?不仅仅是 MVC,还有一些详细的指南。下面是我重写的海盗示例代码。
我在“static void readyThePirates()”中删除了一些 Future 语法,但它仍然运行良好。
提前谢谢你。
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "dart:html";
import 'dart:math' show Random;
import 'dart:convert' show JSON;
import 'dart:async' show Future;
class PirateName {
static final Random indexGen = new Random();
static List<String> names = [];
static List<String> appellations = [];
String _firstName;
String _appellation;
PirateName({String firstName, String appellation}) {
if (firstName == null) {
_firstName = names[indexGen.nextInt(names.length)];
} else {
_firstName = firstName;
}
if (appellation == null) {
_appellation = appellations[indexGen.nextInt(appellations.length)];
} else {
_appellation = appellation;
}
}
PirateName.fromJSON (String jsonString) {
Map storedName = JSON.decode(jsonString);
_firstName = storedName['f'];
_appellation = storedName['a'];
}
String get pirateName => _firstName.isEmpty ? '' : '$_firstName the $_appellation';
String get jsonString => JSON.encode({
"f": _firstName, "a": _appellation
});
/* It's original text in Avast example.
static Future readyThePirates() {
var path = 'piratenames.json';
return HttpRequest.getString(path)
.then(_parsePirateNamesFromJSON);
}
*/
static void readyThePirates() {
var path = 'piratenames.json';
Future future = HttpRequest.getString(path);
future.then((String jsonString) {
Map pirateNames = JSON.decode(jsonString);
names = pirateNames['names'];
appellations = pirateNames['appellations'];
});
}
}
final String TREASURE_KEY = 'pirateName';
void main() {
getBadgeNameFromStorage();
PirateName.readyThePirates();
querySelector('#generateButton').onClick.listen(generateBadge);
querySelector('#clearButton').onClick.listen(clearForm);
querySelector('#jsonButton').onClick.listen(getBadgeNameFromJson);
}
void generateBadge(Event e){
String inputName = querySelector('#inputName').value;
var myPirate = new PirateName(firstName: inputName);
querySelector('#badgeName').text = myPirate.pirateName;
window.localStorage[TREASURE_KEY] = myPirate.jsonString;
if (inputName.trim().isEmpty) {
querySelector('#generateButton')
..disabled = false
..text = 'bra';
} else {
querySelector('#generateButton')
..disabled = true
..text = 'no bra';
}
}
PirateName getBadgeNameFromStorage() {
String storedName = window.localStorage[TREASURE_KEY];
if (storedName != null) {
PirateName newPirate = new PirateName.fromJSON(storedName);
querySelector('#badgeName').text = newPirate.pirateName;
} else {
return null;
}
}
void clearForm(Event e) {
querySelector('#generateButton').disabled = false;
querySelector('#badgeName').text = "";
querySelector('#inputName').value = "";
}
void getBadgeNameFromJson(Event e) {
var jsonPirate = new PirateName();
querySelector('#badgeName').text = jsonPirate._firstName + ' the '+ jsonPirate._appellation;
}
【问题讨论】:
标签: design-patterns return dart future