First Dart Program

In the previous lesson, we set up the Dart environment and finished installing the Dart SDK. In this lesson, we’ll create our very first Dart project.

Creating a New Dart Project

  1. Open CMD terminal, create a folder for your projects and navigate into it. You can make a dedicated folder to store all your Dart projects, for example C:\dartdemo
mkdir dartdemo
cd dartdemo
  1. Run the project generation command below to build a console-based project named firstdart. Feel free to replace firstdart with any project name you like.
dart create firstdart
What This Command Does

dart create firstdart generates a command-line project called firstdart. It automatically creates all standard folders and files, then runs pub get to pull down all required dependencies for you.

Here’s the full list of files generated automatically:

Key Auto-Generated Files
  • .gitignore: Git ignore rules file
  • analysis_options.yaml: Linting and static code analysis configuration
  • pubspec.yaml: Core Dart project config file, stores project metadata and dependency list
  • bin/firstdart.dart: Program entry point file
  • lib/firstdart.dart: Directory for reusable business logic code
  • test/firstdart_test.dart: Unit test source file

Running Your First Dart Program

  1. Move into your newly created project subfolder
cd firstdart 
  1. Execute the run command
dart run
  1. You’ll see the following output if everything runs correctly
Building package executable...
Built cli:cli.
Hello world: 42!Code language: CSS (css)

Editing Code and Rerunning the App

1. File Breakdown

The bin/ folder holds all executable entry scripts, and bin/firstdart.dart acts as the launch file for the whole program.

Every Dart application starts executing from the main() function.

2. Modify the Source Code
import 'package:cli/cli.dart' as cli;

void main(List<String> arguments) {
  print('Hello world: ${cli.calculate()}!');
}
Code language: JavaScript (javascript)

Here’s the edited version — we’ll remove the import statement and update the printed text:

void main(List<String> arguments) {
  print('Hello, Dart!');
}
Code language: JavaScript (javascript)

3. Test Your Changes by Rerunning

Save your file, then run dart run again. Your terminal output will update to this:

Building package executable...
Built cli:cli.
Hello, Dart!Code language: CSS (css)

This confirms your code edits took effect successfully.

Quick Recap of Core Concepts

  1. dart create [project-name]: Generates a fully standardized Dart project template in one command
  2. dart run: Compiles and launches your Dart application
  3. The bin/ directory contains program entry points; the main() function marks the starting point of all Dart code execution
  4. pubspec.yaml is the central configuration file for Dart projects, used to manage dependencies and project metadata

Previous:

Leave a Reply

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