How to Write Dart Code: A Beginner's Tutorial

TL;DR
Dart is a statically typed, compiled language used with the Flutter framework to build cross-platform mobile apps. Every program starts with a main function, uses var for type inference or explicit types like int and string, and runs via the command 'dart filename.dart'. Dart supports both JIT compilation during development and AOT compilation for optimized final builds.
Transcript
Hey there, this Mahmoud Asan and welcome to my Dart programming language tutorial. Dart is the main programming language to develop cross platform mobile application using flutter  framework. In this video tutorial, I will discuss the fundamental features of Dart programming language. If you know any other programming language, then you will f... Read More
Key Insights
- Dart is the main programming language for building cross-platform mobile applications using the Flutter framework, and its concepts are easy to grasp if you already know another programming language.
- Dart is both statically typed and compiled: once a variable is defined as a string, you cannot assign an integer or double to it, though the dynamic type is an exception that can store any kind of value.
- Dart supports two compilation modes: JIT (Just In Time), which compiles code on the fly when you run a program during development, and AOT (Ahead Of Time), which compiles the final product with optimization for deployment.
- Every Dart program starts with a main function written as void main, where void means the function returns nothing; the void keyword is optional since a missing return type also means no return value.
- The var keyword triggers type inference, meaning the Dart compiler automatically guesses a variable's type from its assigned value, so assigning 'Muhammad' to var makes it a string automatically.
- Dart has five basic types: int, double, string, and bool are the four fundamental types, while dynamic is a dynamic type whose value can change to different types at runtime.
- The dart:core package containing built-in types, collections, and core functionality is automatically imported into every Dart program, so you do not need to import it manually.
- String interpolation lets you insert a variable inside a string using the dollar sign followed by the variable name, while concatenation between separate strings uses the plus sign.
Install to Summarize YouTube Videos and Get Transcripts
Explore YouTube Video Summarizer or Get YouTube Transcript Extractor
Questions & Answers
Q: What is Dart used for?
Dart is the main programming language used to develop cross-platform mobile applications with the Flutter framework. According to the tutorial, it is a statically typed and compiled language that supports fast development cycles. The description adds that because Dart supports both AOT and JIT compilation, it delivers extremely fast development cycles along with fast execution and startup times, and it can also transpile code into JavaScript. If you already know another programming language, Dart is easy to understand.
Q: How do you install and set up Dart for development?
To set up Dart, visit the Dart website (dart.dev), find the Get Dart link, and follow the installation instructions for your operating system, which offers Windows, Linux, and Mac versions. The tutorial uses Mac and version 2.3. For writing code, the instructor recommends the Visual Studio Code editor with the Dart extension installed via the Extensions view. Alternatively, you can skip installation entirely and use the DartPad web editor at dartpad.dartlang.org to test code directly in a browser.
Q: What is the difference between JIT and AOT compilation in Dart?
Dart supports two types of compilation. JIT stands for Just In Time compilation, which happens automatically when you run a Dart program during development: the code compiles on the fly and then runs, letting you immediately see output or errors. AOT stands for Ahead Of Time compilation, which is used when you deploy your final product; the code is compiled ahead of time with some optimization. So JIT speeds up development while AOT optimizes the deployed application.
Q: How do you write and run a basic Dart program?
Every Dart program starts with a main function, written as void main, where void indicates the function returns nothing (the void keyword is optional). Inside the function you write statements such as print('HelloWorld'). To run the program, save it with the .dart extension, for example playground.dart, then in the terminal type 'dart playground.dart' and the output appears. Visual Studio Code has a built-in terminal you can open via Terminal then New Terminal, so you do not need a separate window.
Q: What are the basic data types in Dart?
Dart has five basic types: int, double, string, bool, and dynamic. The first four (int, double, string, and bool) are fundamental types, while dynamic is a dynamic type. The dynamic type means you can change the stored value at runtime to different types of values, unlike the statically typed variables that lock to one type. For example, int amount1 = 100 stays an integer, and assigning a double or string to it later would show an error.
Q: What is the difference between var and an explicit type declaration in Dart?
When you declare a variable with var, such as var firstName = 'Muhammad', the Dart compiler automatically guesses the type from the assigned value through a process called type inference, making it a string. When you declare a variable with an explicit type, such as string lastName, you cannot assign a different type like an integer to it or it will show an error. With var, if you assign an integer value it is understood as an integer, so Dart supports both type inference and static typing.
Q: How do you take user input in a Dart program?
To take user input, you must import the dart:io library at the top of your program with 'import dart:io'. Then you use standard output to prompt the user, writing stdout.writeLine('What is your name?'), and capture the response with a string variable assigned to stdin.readLineSync(). When the program reaches the stdin line, it waits for the user to type input. You can then output the value using the print function combined with string interpolation, such as 'My name is $name'.
Q: What comment styles does Dart support?
Dart supports three types of comments. An inline comment uses two forward slashes (//) and applies to a single line. A block comment starts with a forward slash and star and ends with a star and forward slash, allowing multiple lines of comment between them. A documentation comment uses three forward slashes (///) and is used as documentation for your program. These let you annotate code inline, across blocks, or formally document it.
Summary & Key Takeaways
-
To start with Dart, install the Dart SDK from dart.dev (the tutorial uses version 2.3) and optionally the Visual Studio Code editor with the Dart extension. Alternatively, you can test code without installing anything using the DartPad web editor at dartpad.dartlang.org, where you write code and click Run to see output.
-
Dart files use the .dart extension and are executed in the terminal with the command 'dart filename.dart'. Dart is a static-type, compiled language supporting both JIT compilation, which runs code on the fly during development, and AOT compilation, which produces an optimized final product for deployment.
-
Dart programs begin with a main function and support variables defined with var (type inference) or explicit types. It offers three comment styles: // for inline, /* */ for block, and /// for documentation. The dart:io library provides input and output via stdout.writeLine and stdin.readLineSync.
Read in Other Languages (beta)
Share This Summary 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
Explore More Summaries from freeCodeCamp.org 📚




![The Most Important Skills Going Forward with CTO + Homebrew Maintainer Mike McQuaid [Podcast #204] thumbnail](/_next/image?url=https%3A%2F%2Fi.ytimg.com%2Fvi%2F58Tn2xB8kIE%2Fhqdefault.jpg&w=750&q=75)

Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator