在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ 一个简单的 Dart 程序下面的应用程序代码用到了很多 Dart 的基本功能: // Define a function.
void printInteger(int aNumber) {
print('The number is $aNumber.'); // Print to console.
}
// This is where the app starts executing.
void main() {
var number = 42; // Declare and initialize a variable.
printInteger(number); // Call a function.
}
下面是上述应用程序中使用到的代码片段,这些代码片段适用于所有(或几乎所有)的 Dart 应用:
备忘: 本站的代码遵循 Dart 风格指南 中的约定。 重要概念当你在学习 Dart 语言时, 应该牢记以下几点:
关键字下面的表格中列出了 Dart 语言所使用的关键字。
应该避免使用这些单词作为标识符。但是,带有上标的单词可以在必要的情况下作为标识符:
其它没有上标的关键字为 保留字,均不能用作标识符。 变量下面的示例代码将创建一个变量并将其初始化: var name = 'Bob';
变量仅存储对象的引用。这里名为
Object name = 'Bob';
除此之外你也可以指定类型: String name = 'Bob';
备忘: 本文遵循 风格建议指南 中的建议,通过 默认值在 Dart 中,未初始化以及可空类型的变量拥有一个默认的初始值 int? lineCount;
assert(lineCount == null);
备忘:
If you enable null safety, then you must initialize the values of non-nullable variables before you use them: int lineCount = 0;
You don’t have to initialize a local variable where it’s declared, but you do need to assign it a value before it’s used. For example, the following code is valid because Dart can detect that int lineCount;
if (weLikeToCount) {
lineCount = countLines();
} else {
lineCount = 0;
}
print(lineCount);
Top-level and class variables are lazily initialized; the initialization code runs the first time the variable is used. Late variablesDart 2.12 added the
Often Dart’s control flow analysis can detect when a non-nullable variable is set to a non-null value before it’s used, but sometimes analysis fails. Two common cases are top-level variables and instance variables: Dart often can’t determine whether they’re set, so it doesn’t try. If you’re sure that a variable is set before it’s used, but Dart disagrees, you can fix the error by marking the variable as late String description;
void main() {
description = 'Feijoada!';
print(description);
}
If you fail to initialize a When you mark a variable as
In the following example, if the // This is the program's only call to _readThermometer().
late String temperature = _readThermometer(); // Lazily initialized.
Final 和 Const如果你不想更改一个变量,可以使用关键字 备忘: 实例变量 可以是 下面的示例中我们创建并设置两个 final name = 'Bob'; // Without a type annotation
final String nickname = 'Bobby';
你不能修改一个 final 变量的值: name = 'Alice'; // Error: a final variable can only be set once.
使用关键字 const bar = 1000000; // Unit of pressure (dynes/cm2)
const double atm = 1.01325 * bar; // Standard atmosphere
var foo = const [];
final bar = const [];
const baz = []; // Equivalent to `const []`
如果使用初始化表达式为常量赋值可以省略掉关键字 没有使用 final 或 const 修饰的变量的值是可以被更改的,即使这些变量之前引用过 foo = [1, 2, 3]; // Was const []
常量的值不可以被修改: baz = [42]; // Error: Constant variables can't be assigned a value.
你可以在常量中使用 类型检查和强制类型转换 ( const Object i = 3; // Where i is a const Object with an int value...
const list = [i as int]; // Use a typecast.
const map = {if (i is int) i: 'int'}; // Use is and collection if.
const set = {if (list is List<int>) ...list}; // ...and a spread.
备忘: Although a 可以查阅 Lists、Maps 和 Classes 获取更多关于使用 内置类型Dart 语言支持下列内容:
使用字面量来创建对象也受到支持。例如 由于 Dart 中每个变量引用都指向一个对象(一个 类 的实例),通常也可以使用 构造器 来初始化变量。一些内置的类型有它们自己的构造器。例如你可以使用 Some other types also have special roles in the Dart language:
The NumbersDart 支持两种 Number 类型:
整数是不带小数点的数字,下面是一些定义整数字面量的例子: var x = 1;
var hex = 0xDEADBEEF;
var exponent = 8e5;
如果一个数字包含了小数点,那么它就是浮点型的。下面是一些定义浮点数字面量的例子: var y = 1.1;
var exponents = 1.42e5;
You can also declare a variable as a num. If you do this, the variable can have both integer and double values. num x = 1; // x can have both int and double values
x += 2.5;
整型字面量将会在必要的时候自动转换成浮点数字面量: double z = 1; // Equivalent to double z = 1.0.
版本提示: 在 Dart 2.1 之前,在浮点数上下文中使用整数字面量是错误的。 下面是字符串和数字之间转换的方式: // String -> int
var one = int.parse('1');
assert(one == 1);
// String -> double
var onePointOne = double.parse('1.1');
assert(onePointOne == 1.1);
// int -> String
String oneAsString = 1.toString();
assert(oneAsString == '1');
// double -> String
String piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == '3.14');
整型支持传统的位移操作,比如移位( assert((3 << 1) == 6); // 0011 << 1 == 0110
assert((3 | 4) == 7); // 0011 | 0100 == 0111
assert((3 & 4) == 0); // 0011 & 0100 == 0000
更多示例请查看 移位操作符 小节。 数字字面量为编译时常量。很多算术表达式只要其操作数是常量,则表达式结果也是编译时常量。 const msPerSecond = 1000;
const secondsUntilRetry = 5;
const msUntilRetry = secondsUntilRetry * msPerSecond;
更多内容,请查看 Dart 中的数字。 StringsDart 字符串( var s1 = 'Single quotes work well for string literals.';
var s2 = "Double quotes work just as well.";
var s3 = 'It\'s easy to escape the string delimiter.';
var s4 = "It's even easier to use the other delimiter.";
在字符串中,请以 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 s1 = 'String '
'concatenation'
" works even over line breaks.";
assert(s1 ==
'String concatenation works even over '
'line breaks.');
var s2 = 'The + operator ' + 'works, as well.';
assert(s2 == 'The + operator works, as well.');
使用三个单引号或者三个双引号也能创建多行字符串: var s1 = '''
You can create
multi-line strings like this one.
''';
var s2 = """This is also a
multi-line string.""";
|
请发表评论