1. 变量声明
如何定义变量
var name = \'Bob\';
变量的初始值
int lineCount;
assert(lineCount == null); // Variables (even numbers) are initially null.
可以使用var,也可以直接指定类型。
final, 定义为final的变量,值不能够被更改
final name = \'Bob\'; // Or: final String name = \'Bob\';
name = \'Alice\'; // ERROR
2. 基础类型
字符串
字符串可以使用单引号或者双引号。
var s1 = \'Single quotes work well for string literals.\';
var s2 = \"Double quotes work just as well.\";
在字符串中,可以直接应用值, ${表达式}, 如果只是一个变量,就可以去掉{}
var s = \'string interpolation\';
assert(\'Dart has $s, which is very handy.\' ==
\'Dart has string interpolation, which is very handy.\');
assert(\'That deserves all caps. ${s.toUpperCase()} is very handy!\' ==
\'That deserves all caps. STRING INTERPOLATION is very handy!\');
多行字符串,会被认为默认拼接。
var s = \'String \'\'concatenation\'
\" works even over line breaks.\";
assert(s == \'String concatenation works even over line breaks.\');
如果要使用多行字符串,可以这样, 用\'\'\'
var s1 = \'\'\'
You can create
multi-line strings like this one.
\'\'\';
创建一个不考虑转义的字符串
var s = @\"In a raw string, even \\n isn\'t special.\";
StringBuffer, 非常类似.net中的。
var sb = new StringBuffer();
sb.add(\"Use a StringBuffer \");
sb.addAll([\"for \", \"efficient \", \"string \", \"creation \"]);
sb.add(\"if you are \").add(\"building lots of strings.\");
var fullString = sb.toString();