DartBasic 2 min read

Create your first Dart console project

Create a package, find its entry point and verify formatting, analysis and execution.

On this page

A Dart package gives your source code a name, SDK constraints and a place for dependencies. Start with a console project so you can focus on the language before introducing a widget tree.

Create a clean starter

In your projects directory, run:

dart create -t console dart_basics_lab
cd dart_basics_lab
dart run

Read the generated pubspec.yaml and find the entry point under bin/. The exact starter output can change with the SDK; the important behavior is that dart run resolves and runs this package from its root directory.

Replace the small entry point

Open the generated file in bin/ and replace its contents with:

void main() {
  final learner = 'Asha';
  final completed = 3;
  print('$learner completed $completed lessons.');
}

Run it again. The output should be Asha completed 3 lessons.. main is where execution begins. final prevents reassignment of each local variable; string interpolation inserts their current values into the printed message.

Format and analyze

dart format .
dart analyze

Formatting standardizes source layout. Analysis checks types and diagnostics without proving that the program produces the intended result. Execution verifies the behavior for the input you chose. These are complementary steps.

Intentionally change completed to a string and try a numeric operation on it. Read the analyzer diagnostic, undo the change and confirm the original program runs again. Learning to identify the first diagnostic is more useful than pasting an entire error log into unrelated code.

Understand the project files

pubspec.yaml declares the package and constraints. pubspec.lock records resolved dependencies. .dart_tool/ holds generated tooling state; it is not where you write application code. As the program grows, reusable code belongs under lib/ and tests under test/.

Use variables and types next to explain inference, reassignment and compile-time constants. Then practise functions before introducing asynchronous work.

Exercise

Add a remaining count and print a second sentence. Run formatting, analysis and the program. Explain which check would catch a spelling error in a variable name and which check tells you the final message is the one you intended.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.