void main() {
// Get the current date and time.
var now = new DateTime.now();
print(now);
// Create a new DateTime with the local time zone.
var y2k = new DateTime(2000); // January 1, 2000
print(y2k);
// Specify the month and day.
y2k = new DateTime(2000, 1, 2); // January 2, 2...
分类标签归档:Dart
Dart Future
import 'dart:async';
// fake methods to make the below examples work
Future<int> getFuture() async {
return 0;
}
void handleValue(int value) {
print('value is $value');
}
void handleError(int error) {
print('error is $error');
}
Future dbQuery(String query...
Dart JSON解析
import 'dart:convert' show JSON;
void main() {
// NOTE: Be sure to use double quotes ("),
// not single quotes ('), inside the JSON string.
// This string is JSON, not Dart.
var jsonString = '''
[
{"score": 40},
{"score": 80}
]...
Dart迭代器
import 'dart:collection';
class Process {
// Represents a process...
}
class ProcessIterator implements Iterator<Process> {
@override
Process current;
@override
bool moveNext() {
return false;
}
}
// A mythical class that lets you iterate through all
// processes....
Dart Math方法
import 'dart:math' as math;
void main() {
trig();
minmax();
constants();
random();
}
void trig() {
// Cosine
assert(math.cos(math.PI) == -1.0);
// Sine
var degrees = 30;
var radians = degrees * (math.PI / 180);
// radians is now 0.52359.
var sinOf30degrees = math....
Dart数字方法
void main() {
assert(int.parse('42') == 42);
assert(int.parse('0x42') == 66);
assert(double.parse('0.50') == 0.5);
assert(num.parse('42') is int);
assert(num.parse('0x42') is int);
assert(num.parse('0.50') is double);
// Specify the ...
Dart给服务器发送数据
// XXX: Now that it's converted to async-await, we really should run this.
import 'dart:async';
import 'dart:html';
String encodeMap(Map data) {
return data.keys.map((k) {
return '${Uri.encodeComponent(k)}=' + '${Uri.encodeComponent(data[k])}';
}).joi...
Dart Set集合
void main() {
getAndPutItems();
checkForItems();
intersectAndSuperset();
}
void getAndPutItems() {
var ingredients = new Set();
ingredients.addAll(['gold', 'titanium', 'xenon']);
assert(ingredients.length == 3);
// Adding a duplicate item has no effect.
i...
Dart读文件
import 'dart:io';
import 'dart:convert';
import 'dart:async';
void readFileStreamApi() {
var config = new File('config.txt');
Stream<List<int>> inputStream = config.openRead();
inputStream.transform(UTF8.decoder).transform(new LineSplitter()).li...
Dart字符串方法
void main() {
//BEGIN
// Check whether a string contains another string.
assert('Never odd or even'.contains('odd'));
// Does a string start with another string?
assert('Never odd or even'.startsWith('Never'));
// Does a string end with another string...