dart-introduccion-hola-mundo

Introduction to Dart: Installation and Hello World

  • 5 min

Dart is the programming language on which Flutter is built.

We are starting a new journey. If you are here, it’s because you want to learn Flutter, Google’s framework for creating cross-platform applications. But to master Flutter, we first need a solid foundation. That foundation is called Dart.

Dart is a programming language created by Google, optimized for developing user interfaces (UI). It is an object-oriented, strongly typed language with a syntax that, if you come from C#, Java, or JavaScript, will feel incredibly familiar.

In this first post, we are going to prepare the development environment and write our first program. Let’s get to it!

Environment Installation

To program in Dart, we need two fundamental things: the Dart SDK (the development kit) and an IDE (the code editor).

The SDK: Dart or Flutter

Here is the first practical tip. You could install only the Dart SDK, but since our final goal is to build Apps, the smartest thing is to install the Flutter SDK directly.

The Flutter SDK already includes the Dart SDK inside it. This way, we kill two birds with one stone and save ourselves from double configurations in the future.

To install it, go to the official Flutter website and follow the instructions for your operating system (Windows, macOS, or Linux). During the process, you will need to add the SDK’s bin folder to the PATH to be able to run its commands from the terminal.

To verify everything is set up correctly, open a terminal and type:

dart --version
Copied!

If you see something like Dart SDK version 3.x.x, congratulations, you have the engine ready.

VS Code as an Editor

Although Android Studio is very powerful, for learning Dart and developing in Flutter, we prefer Visual Studio Code. It is much lighter, faster, and has a fantastic ecosystem of extensions.

Once you have VS Code installed, install the official extensions from the extensions tab:

  1. Dart: provides language support, autocompletion, and debugging.
  2. Flutter: adds tools for working with the framework (and usually installs the Dart one automatically).

I also recommend installing extensions like “Error Lens” to see errors on the same line of code, or “Material Icon Theme” to have nice file icons. It helps visually.

Our First “Hello World”

Let’s create our first program to understand the basic structure of a Dart script.

Create a folder on your computer for the course, open VS Code in that folder, and create a file named main.dart.

By convention, files in Dart are named in snake_case (all lowercase and separated by underscores).

Write the following code:

void main() {
  print('Hello World from LuisLlamas.es');
}
Copied!

Now, to run it, you have two options:

  1. Press the “Run” button that appears right above the main function in VS Code.
  2. Open the integrated terminal and type dart run main.dart.

In both cases, you will see the output in the console: Hello World from LuisLlamas.es.

Analyzing the Code

It seems simple, but here are already key concepts that define the language:

The main function

In Dart, the main() function is the entry point of the application. When you run a Dart file, the system looks for this specific function to start. If there is no main, the program won’t start.

void main() {
  // Here our program begins
}
Copied!

The keyword void indicates that this function does not return any value.

Code Blocks

As in C, C++, Java, or C#, code blocks are delimited using curly braces { }. Everything inside the main’s braces is what will be executed sequentially.

The print function

It’s a built-in function in the language’s core that prints text to the console. It’s your best friend for debugging while you learn.

The Semicolon

Pay attention to this: Dart uses a semicolon ; at the end of each statement.

Unlike JavaScript or Kotlin where it’s sometimes optional, in Dart it is mandatory. If you forget one, the compiler will yell at you (kindly).

print('Hola')  // ❌ Error: Expected to find ';'
print('Hola'); // ✅ Correct
Copied!

DartPad: The Quick Alternative

If you have issues installing the SDK or just want to test code quickly without opening VS Code, there’s a great tool called DartPad.

It’s an official online editor that allows you to write and run Dart code directly in your browser.

It’s perfect for following along with the examples in this first block of fundamentals without bothering with local installations just yet.

Comments

Finally, something important for documentation. In Dart, comments work the same as in most C-derived languages:

// This is a single-line comment

/*
 This is a multi-line
 comment block
*/
Copied!