Dart 简介
This page provides a brief introduction to the Dart language through samples of its main features.
本页通过 Dart 语言的主要功能示例对 Dart 语言进行了简要介绍。
To learn more about the Dart language, visit the in-depth, individual topic pages listed under Language in the left side menu.
要了解有关 Dart 语言的更多信息,请访问左侧菜单中“语言”下列出的深入的各个主题页面。
For coverage of Dart’s core libraries, check out the library tour. You can also visit the Dart cheatsheet codelab, for a more hands-on introduction.
有关 Dart 核心库的报道,请查看图书馆导览。您还可以访问 Dart 备忘单 Codelab,获取更多动手介绍。
Hello World 世界您好
Every app requires the top-level main()
function, where execution starts. Functions that don’t explicitly return a value have the void
return type. To display text on the console, you can use the top-level print()
function:
每个应用都需要顶级函数,从该 main()
函数开始执行。不显式返回值的函数具有 void
返回类型。若要在控制台上显示文本,可以使用顶级 print()
函数:
void main() {
print('Hello, World!');
}
Read more about the main()
function in Dart, including optional parameters for command-line arguments.
阅读有关 Dart 中 main()
函数的更多信息,包括命令行参数的可选参数。
Variables 变量
Even in type-safe Dart code, you can declare most variables without explicitly specifying their type using var
. Thanks to type inference, these variables’ types are determined by their initial values:
即使在类型安全的 Dart 代码中,也可以声明大多数变量,而无需使用 var
显式指定其类型。由于类型推断,这些变量的类型由其初始值决定:
var name = 'Voyager I';
var year = 1977;
var antennaDiameter = 3.7;
var flybyObjects = ['Jupiter', 'Saturn', 'Uranus', 'Neptune'];
var image = {
'tags': ['saturn'],
'url': '//path/to/saturn.jpg'
};
Read more about variables in Dart, including default values, the final
and const
keywords, and static types.
阅读有关 Dart 中的变量的更多信息,包括默认值、 final
和 const
关键字以及静态类型。
Control flow statements 控制流语句
Dart supports the usual control flow statements:
Dart 支持常用的控制流语句:
if (year >= 2001) {
print('21st century');
} else if (year >= 1901) {
print('20th century');
}
for (final object in flybyObjects) {
print(object);
}
for (int month = 1; month <= 12; month++) {
print(month);
}
while (year < 2016) {
year += 1;
}
Read more about control flow statements in Dart, including break
and continue
, switch
and case
, and assert
.
阅读有关 Dart 中的控制流语句的更多信息,包括 break
和 、 switch
和 case
assert
continue
。
Functions 功能
We recommend specifying the types of each function’s arguments and return value:
建议指定每个函数的参数和返回值的类型:
int fibonacci(int n) {
if (n == 0 || n == 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
var result = fibonacci(20);
A shorthand =>
(arrow) syntax is handy for functions that contain a single statement. This syntax is especially useful when passing anonymous functions as arguments:
速记 =>
(箭头)语法对于包含单个语句的函数非常方便。当将匿名函数作为参数传递时,此语法特别有用:
flybyObjects.where((name) => name.contains('turn')).forEach(print);
Besides showing an anonymous function (the argument to where()
), this code shows that you can use a function as an argument: the top-level print()
function is an argument to forEach()
.
除了显示匿名函数(参数 的参数)之外,此代码还显示您可以使用函数作为参数:顶级 print()
函数是 的 forEach()
参数 where()
。
Read more about functions in Dart, including optional parameters, default parameter values, and lexical scope.