Skip to content

Commit fc8ff86

Browse files
[完成翻译] src/content/get-started/fundamentals/dart.md (#1522)
Co-authored-by: Amos <[email protected]>
1 parent 8effc69 commit fc8ff86

File tree

1 file changed

+96
-4
lines changed
  • src/content/get-started/fundamentals

1 file changed

+96
-4
lines changed

src/content/get-started/fundamentals/dart.md

+96-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
---
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 编程语言
46
prev:
5-
title: Fundamentals
7+
# title: Fundamentals
8+
title: 基础知识
69
path: /get-started/fundamentals
710
next:
811
title: Widgets
@@ -19,16 +22,31 @@ code examples, feel free to skip this page.
1922
You do not need to be an expert in Dart to
2023
continue with this series.
2124

25+
要开始使用 Flutter,
26+
你需要对 Dart 编程语言有所了解,
27+
因为 Flutter 应用程序就是用 Dart 编写的。
28+
本页面是对 Dart 的简要介绍,如果你能轻松理解代码示例,可以跳过此页。
29+
你不需要成为 Dart 专家,就可以继续后续的内容。
30+
2231
## Dart
2332

2433
Flutter applications are built in [Dart][],
2534
a language that will look familiar
2635
to anyone who's written Java, Javascript,
2736
or any other C-like language.
2837

38+
Flutter 应用程序是用 [Dart][] 编写的,
39+
对于曾经写过 Java、Javascript 或其他类似 C 风格语言的人来说,
40+
这种语言应该很熟悉。
41+
2942
:::note
43+
3044
Installing Flutter also installs Dart,
3145
so you don't need to install Dart separately.
46+
47+
安装 Flutter 时也会同时安装 Dart,
48+
因此你无需单独安装 Dart。
49+
3250
:::
3351

3452
The following example is a small program that
@@ -39,6 +57,11 @@ If you're confident in your ability to
3957
understand this program,
4058
feel free to skip to the page.
4159

60+
以下是一个小型示例程序,它从 dart.dev 获取数据,
61+
解码返回的 JSON,并将其打印到控制台。
62+
如果你感觉自己已经能够理解该程序,
63+
那就可以跳过这一页。
64+
4265
```dart
4366
import 'dart:convert';
4467
import 'package:http/http.dart' as http;
@@ -73,11 +96,17 @@ This program has two parts:
7396
the `Package` class declaration, and the business logic,
7497
which is contained in the [`main`][] function.
7598

99+
这个程序分为两部分:
100+
`Package` 类的声明,以及包含业务逻辑的 [`main`][] 函数。
101+
76102
The `Package` class contains many of the most common
77103
features you'll use when working with [classes in Dart][].
78104
This class has three members,
79105
and defines a constructor and a method.
80106

107+
`Package` 类包含了你在 [Dart 中使用类][classes in Dart] 时最常用的一些特性。
108+
这个类有三个成员,并定义了一个构造函数和一个方法。
109+
81110
The Dart language is [type safe][]; it uses
82111
static type checking to ensure that
83112
a variable's value always matches the
@@ -90,6 +119,14 @@ there are many lines that start with `final variableName =`.
90119
These lines are type safe,
91120
despite not being explicitly given a type.
92121

122+
Dart 是 [类型安全][type safe] 的编程语言;
123+
它使用静态类型检查来确保变量的值始终与变量的静态类型相匹配。
124+
在定义类时,通常是必须给成员加上 `String` 类型标注,
125+
但由于类型推断的存在,这一步变得可选了。
126+
在这个例子的 `main` 函数里,
127+
许多行以 `final variableName =` 开头。
128+
尽管这些行没有显式指定类型,但它们依然是类型安全的。
129+
93130
Dart also has built-in [sound null safety][].
94131
In the example, the `description` member is
95132
declared with the type `String?`.
@@ -102,6 +139,14 @@ You can see this demonstrated in the constructor for
102139
the `Package` class. It takes two required,
103140
positional arguments and one optional, named argument.
104141

142+
Dart 内置了 [健全的空安全][sound null safety]
143+
在这个例子中,`description` 成员的类型被声明为 `String?`
144+
`?` 表示该属性可以为 null。
145+
而其它两个成员则不能为 null,
146+
如果你尝试将它们设置为 null,程序将无法编译。
147+
你可以在 `Package` 类的构造函数中看到这一点。
148+
该构造函数接受两个必需的位置参数和一个可选的命名参数。
149+
105150
Next in the example is the `main` function.
106151
All Dart programs, including Flutter apps,
107152
start with a `main` function.
@@ -110,12 +155,23 @@ including using libraries, marking functions as async,
110155
making function calls, using `if` statement control-flow,
111156
and more.
112157

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+
114167
The main entrypoint in a starter
115168
Flutter app is in `lib/main.dart`.
116169
The default `main` method looks
117170
like the following:
118171

172+
Flutter 应用的主入口点是在 `lib/main.dart` 中。
173+
默认的 `main` 方法如下所示:
174+
119175
```dart title="lib/main.dart"
120176
void main() {
121177
runApp(const MyApp());
@@ -133,24 +189,49 @@ the [`FutureBuilder`][] API, [Deferred components][],
133189
or the [Working with long lists][] cookbook recipe,
134190
as appropriate.
135191

192+
在调用 `runApp()` _之前_
193+
可以执行任何 _快速_ 的初始化(少于一两帧的时间),
194+
但要注意,这时 widget 树还未创建。
195+
如果你需要进行较长时间的初始化,
196+
比如从磁盘或网络加载数据,
197+
请确保以不会阻塞主 UI 线程的方式进行。
198+
更多相关信息,请具体根据需要来参考 [异步编程][Asynchronous programming]
199+
[`FutureBuilder`][] API、[延迟加载组件][Deferred components]
200+
[处理长列表][Working with long lists] 的实用教程 (Cookbook)。
201+
136202
Every stateful widget has an `initState()`
137203
method that is called when the widget is
138204
created and added to the widget tree.
139205
You can override this method and perform
140206
initialization there, though the first line of
141207
this method _must_ be `super.initState()`.
142208

209+
每个 stateful widget 都有一个 `initState()` 方法,
210+
它会在 widget 创建并添加到 widget 树时调用。
211+
你可以重写这个方法并在其中进行初始化,
212+
但这个方法的第一行 _必须_`super.initState()`
213+
143214
Finally, hot reloading your app does _not_
144215
call `initState` or `main` again.
145216
Hot restart calls both.
217+
218+
最后,需要注意,
219+
热重载应用 _不会_ 再次调用 `initState``main`
220+
但热重启会调用这两者。
221+
146222
:::
147223

148224
If these features aren't familiar to you,
149225
you can find resources to learn Dart on the
150226
[Bootstrap into Dart][] page.
151227

228+
如果这些特性对你来说不太熟悉,你可以在
229+
[Dart 语言指引][Bootstrap into Dart] 页面上找到相关资源。
230+
152231
## Next: Widgets
153232

233+
## 下一步:Widget
234+
154235
This page is an introduction to Dart,
155236
and helps you become familiar with reading
156237
Flutter and Dart code. It's okay if you don't
@@ -160,6 +241,12 @@ of the Dart language.
160241
In the next section, you'll learn about the
161242
building block of Flutter apps: widgets.
162243

244+
本页面介绍了 Dart,
245+
并帮助你熟悉阅读 Flutter 和 Dart 代码。
246+
如果你对本页的所有代码不太清楚也没关系,
247+
重要的是你能对 Dart 语言的 _语法_ 感到熟悉。
248+
在下一部分,你将学习 Flutter 应用程序的构建模块:widget。
249+
163250
[Asynchronous programming]: {{site.dart-site}}/libraries/async/async-await
164251
[Dart]: {{site.dart-site}}
165252
[Deferred components]: /perf/deferred-components
@@ -173,7 +260,12 @@ building block of Flutter apps: widgets.
173260

174261
## Feedback
175262

263+
## 反馈
264+
176265
As this section of the website is evolving,
177266
we [welcome your feedback][]!
178267

268+
由于本网站的此部分正在不断发展,
269+
我们 [欢迎你的反馈][welcome your feedback]
270+
179271
[welcome your feedback]: https://google.qualtrics.com/jfe/form/SV_6A9KxXR7XmMrNsy?page="dart"

0 commit comments

Comments
 (0)