1
1
---
2
- title : Intro to Dart
3
- description : Learn about the Dart programming language
2
+ # title: Intro to Dart
3
+ title : Dart 入门
4
+ # description: Learn about the Dart programming language
5
+ description : 了解 Dart 编程语言
4
6
prev :
5
- title : Fundamentals
7
+ # title: Fundamentals
8
+ title : 基础知识
6
9
path : /get-started/fundamentals
7
10
next :
8
11
title : Widgets
@@ -19,16 +22,31 @@ code examples, feel free to skip this page.
19
22
You do not need to be an expert in Dart to
20
23
continue with this series.
21
24
25
+ 要开始使用 Flutter,
26
+ 你需要对 Dart 编程语言有所了解,
27
+ 因为 Flutter 应用程序就是用 Dart 编写的。
28
+ 本页面是对 Dart 的简要介绍,如果你能轻松理解代码示例,可以跳过此页。
29
+ 你不需要成为 Dart 专家,就可以继续后续的内容。
30
+
22
31
## Dart
23
32
24
33
Flutter applications are built in [ Dart] [ ] ,
25
34
a language that will look familiar
26
35
to anyone who's written Java, Javascript,
27
36
or any other C-like language.
28
37
38
+ Flutter 应用程序是用 [ Dart] [ ] 编写的,
39
+ 对于曾经写过 Java、Javascript 或其他类似 C 风格语言的人来说,
40
+ 这种语言应该很熟悉。
41
+
29
42
::: note
43
+
30
44
Installing Flutter also installs Dart,
31
45
so you don't need to install Dart separately.
46
+
47
+ 安装 Flutter 时也会同时安装 Dart,
48
+ 因此你无需单独安装 Dart。
49
+
32
50
:::
33
51
34
52
The following example is a small program that
@@ -39,6 +57,11 @@ If you're confident in your ability to
39
57
understand this program,
40
58
feel free to skip to the page.
41
59
60
+ 以下是一个小型示例程序,它从 dart.dev 获取数据,
61
+ 解码返回的 JSON,并将其打印到控制台。
62
+ 如果你感觉自己已经能够理解该程序,
63
+ 那就可以跳过这一页。
64
+
42
65
``` dart
43
66
import 'dart:convert';
44
67
import 'package:http/http.dart' as http;
@@ -73,11 +96,17 @@ This program has two parts:
73
96
the ` Package ` class declaration, and the business logic,
74
97
which is contained in the [ ` main ` ] [ ] function.
75
98
99
+ 这个程序分为两部分:
100
+ ` Package ` 类的声明,以及包含业务逻辑的 [ ` main ` ] [ ] 函数。
101
+
76
102
The ` Package ` class contains many of the most common
77
103
features you'll use when working with [ classes in Dart] [ ] .
78
104
This class has three members,
79
105
and defines a constructor and a method.
80
106
107
+ ` Package ` 类包含了你在 [ Dart 中使用类] [ classes in Dart ] 时最常用的一些特性。
108
+ 这个类有三个成员,并定义了一个构造函数和一个方法。
109
+
81
110
The Dart language is [ type safe] [ ] ; it uses
82
111
static type checking to ensure that
83
112
a variable's value always matches the
@@ -90,6 +119,14 @@ there are many lines that start with `final variableName =`.
90
119
These lines are type safe,
91
120
despite not being explicitly given a type.
92
121
122
+ Dart 是 [ 类型安全] [ type safe ] 的编程语言;
123
+ 它使用静态类型检查来确保变量的值始终与变量的静态类型相匹配。
124
+ 在定义类时,通常是必须给成员加上 ` String ` 类型标注,
125
+ 但由于类型推断的存在,这一步变得可选了。
126
+ 在这个例子的 ` main ` 函数里,
127
+ 许多行以 ` final variableName = ` 开头。
128
+ 尽管这些行没有显式指定类型,但它们依然是类型安全的。
129
+
93
130
Dart also has built-in [ sound null safety] [ ] .
94
131
In the example, the ` description ` member is
95
132
declared with the type ` String? ` .
@@ -102,6 +139,14 @@ You can see this demonstrated in the constructor for
102
139
the ` Package ` class. It takes two required,
103
140
positional arguments and one optional, named argument.
104
141
142
+ Dart 内置了 [ 健全的空安全] [ sound null safety ] 。
143
+ 在这个例子中,` description ` 成员的类型被声明为 ` String? ` ,
144
+ ` ? ` 表示该属性可以为 null。
145
+ 而其它两个成员则不能为 null,
146
+ 如果你尝试将它们设置为 null,程序将无法编译。
147
+ 你可以在 ` Package ` 类的构造函数中看到这一点。
148
+ 该构造函数接受两个必需的位置参数和一个可选的命名参数。
149
+
105
150
Next in the example is the ` main ` function.
106
151
All Dart programs, including Flutter apps,
107
152
start with a ` main ` function.
@@ -110,12 +155,23 @@ including using libraries, marking functions as async,
110
155
making function calls, using ` if ` statement control-flow,
111
156
and more.
112
157
113
- :::note Where does initialization code go?
158
+ 接下来是 ` main ` 函数。
159
+ 所有 Dart 程序,包括 Flutter 应用程序,
160
+ 都是从 ` main ` 函数开始的。
161
+ 该函数展示了 Dart 语言的一些基本特性,
162
+ 包括使用库、标记函数为异步、调用函数、使用 ` if ` 语句控制流等等。
163
+
164
+ :::note 初始化的代码应该放在哪?
165
+ <!-- Where does initialization code go? -->
166
+
114
167
The main entrypoint in a starter
115
168
Flutter app is in ` lib/main.dart ` .
116
169
The default ` main ` method looks
117
170
like the following:
118
171
172
+ Flutter 应用的主入口点是在 ` lib/main.dart ` 中。
173
+ 默认的 ` main ` 方法如下所示:
174
+
119
175
``` dart title="lib/main.dart"
120
176
void main() {
121
177
runApp(const MyApp());
@@ -133,24 +189,49 @@ the [`FutureBuilder`][] API, [Deferred components][],
133
189
or the [ Working with long lists] [ ] cookbook recipe,
134
190
as appropriate.
135
191
192
+ 在调用 ` runApp() ` _ 之前_ ,
193
+ 可以执行任何 _ 快速_ 的初始化(少于一两帧的时间),
194
+ 但要注意,这时 widget 树还未创建。
195
+ 如果你需要进行较长时间的初始化,
196
+ 比如从磁盘或网络加载数据,
197
+ 请确保以不会阻塞主 UI 线程的方式进行。
198
+ 更多相关信息,请具体根据需要来参考 [ 异步编程] [ Asynchronous programming ] 、
199
+ [ ` FutureBuilder ` ] [ ] API、[ 延迟加载组件] [ Deferred components ] 、
200
+ 或 [ 处理长列表] [ Working with long lists ] 的实用教程 (Cookbook)。
201
+
136
202
Every stateful widget has an ` initState() `
137
203
method that is called when the widget is
138
204
created and added to the widget tree.
139
205
You can override this method and perform
140
206
initialization there, though the first line of
141
207
this method _ must_ be ` super.initState() ` .
142
208
209
+ 每个 stateful widget 都有一个 ` initState() ` 方法,
210
+ 它会在 widget 创建并添加到 widget 树时调用。
211
+ 你可以重写这个方法并在其中进行初始化,
212
+ 但这个方法的第一行 _ 必须_ 是 ` super.initState() ` 。
213
+
143
214
Finally, hot reloading your app does _ not_
144
215
call ` initState ` or ` main ` again.
145
216
Hot restart calls both.
217
+
218
+ 最后,需要注意,
219
+ 热重载应用 _ 不会_ 再次调用 ` initState ` 或 ` main ` 。
220
+ 但热重启会调用这两者。
221
+
146
222
:::
147
223
148
224
If these features aren't familiar to you,
149
225
you can find resources to learn Dart on the
150
226
[ Bootstrap into Dart] [ ] page.
151
227
228
+ 如果这些特性对你来说不太熟悉,你可以在
229
+ [ Dart 语言指引] [ Bootstrap into Dart ] 页面上找到相关资源。
230
+
152
231
## Next: Widgets
153
232
233
+ ## 下一步:Widget
234
+
154
235
This page is an introduction to Dart,
155
236
and helps you become familiar with reading
156
237
Flutter and Dart code. It's okay if you don't
@@ -160,6 +241,12 @@ of the Dart language.
160
241
In the next section, you'll learn about the
161
242
building block of Flutter apps: widgets.
162
243
244
+ 本页面介绍了 Dart,
245
+ 并帮助你熟悉阅读 Flutter 和 Dart 代码。
246
+ 如果你对本页的所有代码不太清楚也没关系,
247
+ 重要的是你能对 Dart 语言的 _ 语法_ 感到熟悉。
248
+ 在下一部分,你将学习 Flutter 应用程序的构建模块:widget。
249
+
163
250
[ Asynchronous programming ] : {{site.dart-site}}/libraries/async/async-await
164
251
[ Dart ] : {{site.dart-site}}
165
252
[ Deferred components ] : /perf/deferred-components
@@ -173,7 +260,12 @@ building block of Flutter apps: widgets.
173
260
174
261
## Feedback
175
262
263
+ ## 反馈
264
+
176
265
As this section of the website is evolving,
177
266
we [ welcome your feedback] [ ] !
178
267
268
+ 由于本网站的此部分正在不断发展,
269
+ 我们 [ 欢迎你的反馈] [ welcome your feedback ] !
270
+
179
271
[ welcome your feedback ] : https://google.qualtrics.com/jfe/form/SV_6A9KxXR7XmMrNsy?page="dart"
0 commit comments