目录

Contents

起步教程:编写命令行和服务端应用

跟着下面这些步骤开始使用 Dart SDK 来开发命令行和服务器应用。首先你将在浏览器中运行 Dart 编程语言和库而不需要下载它。接着,你需要安装 Dart SDK 并尝试开发一个小程序,然后使用 Dart VM 运行它。最后你将使用一个 AOT()编译器将你刚才完成的程序编译为可以被 Dart 运行时执行的原生机器码。

Follow these steps to start using the Dart SDK to develop command-line and server apps. First you’ll play with the Dart language and libraries in your browser, no download required. Then you’ll install the Dart SDK, write a small program, and run that program using the Dart VM. Finally, you’ll use an AOT (ahead of time) compiler to compile your finished program to native machine code, which you’ll execute using the Dart runtime.

1. 在 DartPad 中运行 Dart 代码

1. Play with Dart code in DartPad

你可以使用 DartPad 来简单地尝试 Dart 编程语言和 API 且不需要下载任何东西。

With DartPad you can experiment with the Dart language and APIs, no download necessary.

例如,下面这个内嵌的 DartPad 可以让你尝试一个简单的 Hello World 程序代码。点击运行来运行应用;控制台输出的内容位于代码块下方。你可以尝试更改源代码,比如更改问候语或者其它的一些语句。你也可以 在 dartpad.dev 中打开示例 以获取更完整的 DartPad 体验。

For example, here’s an embedded DartPad that lets you play with the code for a small Hello World program. Click Run to run the app; the console output appears beneath the code. Try editing the source code—perhaps you’d like to change the greeting to use another language. To get the full DartPad experience, open the example at dartpad.dev.

更多信息:

More information:

2. 安装 Dart

2. Install Dart

Once you’re ready to move beyond DartPad and develop real apps, you need the Dart SDK.

Use Chocolatey to install a stable release of the Dart SDK:

  C:\> choco install dart-sdk

You can use Aptitude to install the Dart SDK on Linux.

  1. Perform the following one-time setup:
    $ sudo apt-get update
    $ sudo apt-get install apt-transport-https
    $ sudo sh -c 'curl https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -'
    $ sudo sh -c 'curl https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list > /etc/apt/sources.list.d/dart_stable.list'
    
  2. Install the Dart SDK:
    $ sudo apt-get update
    $ sudo apt-get install dart
    

With Homebrew, installing Dart is easy.

  $ brew tap dart-lang/dart
  $ brew install dart

3. 获取更多命令行开发工具

3. Get more command-line developer tools

安装 stagehand,该工具可以让你在创建 Dart 应用时提供模板:

Install stagehand, which gives you templates for creating Dart apps:

$ pub global activate stagehand

注意尽管这些指令以命令行的形式存在,但是许多 IDE 也支持使用这些指令进行 Dart 开发。当你创建一个新的 Dart 项目时,那些 IDE 在底层依然使用 Stagehand 来进行创建。

Note that although these instructions feature the command line, many IDEs support Dart development. Those IDEs use Stagehand behind the scenes when you create new Dart projects.

更多信息:

More information:

4. 创建一个小应用

4. Create a small app

创建一个命令行应用:

Create a command-line app:

$ mkdir cli
$ cd cli
$ stagehand console-full

这些命令创建一个包含下述信息的小 Dart 应用:

These commands create a small Dart app that has the following:

  • 一个主要的 Dart 源文件,bin/main.dart,该文件包含一个顶层 main() 函数。该函数是你应用的入口。

    A main Dart source file, bin/main.dart, that contains a top-level main() function. This is the entrypoint for your app.

  • 一个额外的 Dart 文件,lib/cli.dart,包含一些功能性的函数方法,这些函数方法将会导入到 main.dart 文件中。

    An additional Dart file, lib/cli.dart, that contains the functionality of the app and is imported by the main.dart file.

  • 一个 pubspec 文件,pubspec.yaml,包含应用的元数据,包括应用依赖的 信息以及所需的版本等。

    A pubspec file, pubspec.yaml, that contains the app’s metadata, including information about which packages the app depends on and which versions of those packages are required.

5. 获取应用的依赖

5. Get the app’s dependencies

使用 pub 命令获取应用依赖的包:

Use the pub command to get the packages that the app depends on:

$ pub get

6. 运行应用

6. Run the app

为了从命令行运行应用,使用 dart 命令运行 Dart VM:

To run the app from the command line, use the Dart VM by running the dart command:

$ dart bin/main.dart
Hello world: 42!

If you wish run the app with debugging support, see DevTools.

7. 修改应用

7. Modify the app

现在我们来自定义刚才你所创建的应用。

Let’s customize the app you just created.

  1. 编辑 lib/cli.dart 以返回一个不同的结果。例如,将先前的值除以2。(关于 ~/ 的详情请查看 Arithmetic operators):

    Edit lib/cli.dart to calculate a different result. For example, divide the previous value by two (for details about ~/, see Arithmetic operators):

    int calculate() {
      return 6 * 7 ~/ 2;
    }
  2. 保存你刚才所做的改变。

    Save your changes.

  3. 重新运行你应用的入口 main 函数:

    Rerun the main entrypoint of your app:

    $ dart bin/main.dart
    Hello world: 21!
    

更多信息:开发命令行应用

More information: Write command-line apps

8. 编译成正式产品

8. Compile for production

上面的示例步骤我们使用的是 Dart VM(即 dart 命令)运行的应用。Dart VM 针对快速增量编译进行了优化,以便在开发过程中提供即时的响应。现在你的小应用已经完成,是时候 AOT 优化编译你的 Dart 代码为原生机器代码了。

The steps above used the Dart VM (dart) to run the app. The Dart VM is optimized for fast, incremental compilation to provide instant feedback during development. Now that your small app is done, it’s time to AOT compile your Dart code to optimized native machine code.

使用 dart2aot 工具将程序 AOT 编译成机器代码:

Use the dart2aot tool to AOT compile the program to machine code:

$ dart2aot bin/main.dart bin/main.dart.aot

为了运行编译后的程序,使用 Dart 运行时(即 dartaotruntime 命令):

Use the dart2native tool to AOT compile the program to machine code:

$ dart2native bin/main.dart -o bin/my_app

注意测量编译后的程序启动有多快:

Notice how the compiled program starts instantly, completing quickly:

$ time bin/my_app
Hello world: 21!

real	0m0.016s
user	0m0.008s
sys	0m0.006s

接下来做什么?

What next?

检索这些资源:

Check out these resources:

如果你卡住了,可以从 社区和帮助 中查找帮助。

If you get stuck, find help at Community and support.