Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

dart.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more
Get started: Command-line and server apps

Dart 3.10 is taking off with dot shorthands, stable build hooks, nuanced deprecation annotations, and more!Learn more

Follow these steps to start using the Dart SDK to develop command-line and server apps. First you'll play with the Dart language 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. Play with Dart code in DartPad

#

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

For example, here's an embedded DartPad that lets you play with the code for a small Hello World program. ClickRun to run the app; output appears in the console view. Try editing the source code—perhaps you'd like to change the greeting to use another language.

Note
void main() {  print('Hello, World!');}

More information:

2. Install Dart

#

To develop Dart apps, you need the Dart SDK. To continue with this guide, eitherdownload the Dart SDK orinstall Flutter, which includes the full Dart SDK.

3. Create a small app

#

Use thedart create command and theconsole template to create a command-line app:

console
$ dart create -t console cli

This command creates a small Dart app that has the following:

  • A main Dart source file,bin/cli.dart, that contains a top-levelmain() function. This is the entrypoint for your app.
  • An additional Dart file,lib/cli.dart, that contains the functionality of the app and is imported by thecli.dart file.
  • A pubspec file,pubspec.yaml, that contains the app's metadata, including information about whichpackages the app depends on and which versions of those packages are required.
Note

Under the hood,dart create runsdart pub get, which scans the generated pubspec file and downloads dependencies. If you add other dependencies to your pubspec file, then rundart pub get to download them.

4. Run the app

#

To run the app from the command line, use the Dart VM by running thedart run command in the app's top directory:

console
$ cd cli$ dart runHello world: 42!

If you want to run the app with debugging support, seeDart DevTools.

5. Modify the app

#

Let's customize the app you just created.

  1. Editlib/cli.dart to calculate a different result. For example, divide the previous value by two (for details about~/, seeArithmetic operators):

    dart
    intcalculate(){return6*7~/2;}
  2. Save your changes.

  3. Rerun the main entrypoint of your app:

    console
    $ dart runHello world: 21!

More information:Write command-line apps

6. Compile for production

#

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.

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

console
$ dart compile exe bin/cli.dart

Notice how the compiled program starts instantly, completing quickly:

console
$ time bin/cli.exeHello world: 21!real0m0.016suser0m0.008ssys0m0.006s

What next?

#

Check out these resources:

If you get stuck, find help atCommunity and support.

Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Dart 3.10.0. Page last updated on 2025-10-18.View source orreport an issue.


[8]ページ先頭

©2009-2025 Movatter.jp