Skip to content

Dart | Working with the DartCommand Line

  • Ralph 

Introduction

Dart Command Line

To show all commands, just run dart

❯ dart
A command-line utility for Dart development.

Usage: dart <command|dart-file> [arguments]

Global options:
-h, --help                 Print this usage information.
-v, --verbose              Show additional command output.
    --version              Print the Dart SDK version.
    --enable-analytics     Enable analytics.
    --disable-analytics    Disable analytics.

Available commands:
  analyze    Analyze Dart code in a directory.
  compile    Compile Dart to various formats.
  create     Create a new Dart project.
  devtools   Open DevTools (optionally connecting to an existing application).
  doc        Generate API documentation for Dart projects.
  fix        Apply automated fixes to Dart source code.
  format     Idiomatically format Dart source code.
  migrate    Perform null safety migration on a project.
  pub        Work with packages.
  run        Run a Dart program.
  test       Run tests for a project.

Create a Dart project

❯ dart create
Create a new Dart project.

Usage: dart create [arguments] <directory>
-h, --help                       Print this usage information.
-t, --template                   The project template to use.

          [console] (default)    A command-line application.
          [package]              A package containing shared Dart libraries.
          [server-shelf]         A server app using package:shelf.
          [web]                  A web app that uses only core Dart libraries.

    --[no-]pub                   Whether to run 'pub get' after the project has been created.
                                 (defaults to on)
    --force                      Force project generation, even if the target directory already exists.

Create a Server App

Create the Project

❯ dart create app_server -t server-shelf
Creating app_server using template server-shelf...

  .gitignore
  analysis_options.yaml
  CHANGELOG.md
  pubspec.yaml
  README.md
  Dockerfile
  .dockerignore
  test\server_test.dart
  bin\server.dart

Running pub get...
  Resolving dependencies...
  Downloading lints 2.0.0...
  Downloading shelf_router 1.1.3...
  Downloading http_methods 1.1.0...
  Downloading test 1.21.2...
  Downloading test_core 0.4.14...
  Downloading test_api 0.4.10...
  Downloading glob 2.1.0...
  Downloading frontend_server_client 2.1.3...
  Downloading analyzer 4.1.0...
  Downloading _fe_analyzer_shared 40.0.0...
  Downloading coverage 1.3.2...
  Changed 49 dependencies!

Created project app_server in app_server! In order to get started, run the following commands:

  cd app_server
  dart run bin/server.dart

Run the App

❯ cd app_server
❯ dart run bin/server.dart

Create a Web App

Create the Project

❯ dart create app_web -t web
Creating app_web using template web...

  .gitignore
  analysis_options.yaml
  CHANGELOG.md
  pubspec.yaml
  README.md
  web\index.html
  web\main.dart
  web\styles.css

Running pub get...
  Resolving dependencies...
  Downloading build_web_compilers 3.2.3...
  Downloading protobuf 2.1.0...
  Downloading build_modules 4.0.5...
  Downloading build_runner 2.1.11...
  Downloading matcher 0.12.12...
  Downloading build 2.3.0...
  Downloading dart_style 2.2.3...
  Downloading build_resolvers 2.0.9...
  Changed 58 dependencies!

Created project app_web in app_web! In order to get started, run the following commands:

  cd app_web
  dart pub global activate webdev
  webdev serve

Run the App

❯ cd app_web
❯ dart pub global activate webdev
Package webdev is currently active at version 2.7.9.
Resolving dependencies...
The package webdev is already activated at newest available version.
To recompile executables, first run `dart pub global deactivate webdev`.
Installed executable webdev.
Activated webdev 2.7.9.
❯ webdev serve

Serve with different Port as the default port 8080

❯ webdev serve web:10000
[INFO] Building new asset graph completed, took 11.2s
[INFO] Checking for unexpected pre-existing outputs. completed, took 13ms
[INFO] Serving `web` on http://127.0.0.1:10000
[WARNING] No actions completed for 51.9s, waiting on:
  - build_web_compilers:entrypoint on web/main.dart

[INFO] Generating SDK summary completed, took 49.3s
[WARNING] No actions completed for 15.0s, waiting on:
  - build_web_compilers:sdk_js on asset:build_web_compilers/$package$
  - build_web_compilers:entrypoint on web/main.dart

[WARNING] No actions completed for 15.0s, waiting on:
  - build_web_compilers:sdk_js on asset:build_web_compilers/$package$
  - build_web_compilers:entrypoint on web/main.dart

[INFO] Running build completed, took 1m 40s
[INFO] Caching finalized dependency graph completed, took 1.7s
[INFO] Succeeded after 1m 42s with 15 outputs (1363 actions)
[INFO] ---------------------------------------------------------------------

Create a Command-line App

Create the Project

❯ dart create app_console -t console
Creating app_console using template console...

  .gitignore
  analysis_options.yaml
  CHANGELOG.md
  pubspec.yaml
  README.md
  bin\app_console.dart
  lib\app_console.dart
  test\app_console_test.dart

Running pub get...
  Resolving dependencies...
  Changed 46 dependencies!

Created project app_console in app_console! In order to get started, run the following commands:

  cd app_console
  dart run

Run the App

❯ cd app_console
❯ dart run
Building package executable...
Built app_console:app_console.
Hello world: 42!

Build an executable for Windows

❯ dart compile exe .\bin\main.dart
Info: Compiling with sound null safety
Generated: ...\app_console\bin\main.exe
❯ dir bin

    Directory: D:\CLOUD\Programmier-Workshops\Kurse\Dart\Einsteiger\Overview\app_console\bin

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          21.06.2022    10:28            329 main.dart
-a---          21.06.2022    10:33        5025280 main.exe

❯ .\bin\main.exe
Hello world: 42!

Use a different App Name as Executable:

  • Use option -o with dart compile
❯ dart compile exe .\bin\main.dart -o starter.exe
❯ .\bin\starter.exe
  • configure name in pubspec.yaml
executables:
  github_activity: gh_activity

Leave a Reply

Your email address will not be published. Required fields are marked *