DEV Community

Cover image for LEARNING BASIC DART
Raphael Kihiro
Raphael Kihiro

Posted on

LEARNING BASIC DART

Introduction to dart
Dart is a modern, object-oriented programming language developed by Google. It’s designed to be easy to learn, fast, and versatile, powering everything from mobile apps (using Flutter) to web and backend development.

From my experience, Dart’s syntax feels very familiar if you’ve worked with languages like JavaScript; it’s clean, intuitive, and beginner friendly. At the same time, it offers powerful features that make it an excellent choice for both beginners learning programming concepts and professionals building large-scale applications.

Why Learn Dart
From what I have gather in my first week learning the language:

  • Dart is a modern, object-oriented programming language developed by Google as an improvement over the popular JavaScript. It’s designed to be fast, powerful, and easy to learn, making it ideal for everything from web to backend and mobile app development.

  • Dart is a beast when it comes to building highly optimized backend systems especially in the hands of a skilled developer who has mastered its tools and features.

  • It also powers Flutter, one of the world’s most popular frameworks for cross-platform app development, allowing developers to build beautiful and efficient apps for Android, iOS, web, and desktop.

  • Learning Dart is a smart move for anyone interested in modern backend development. With Serverpod, Dart provides a clean, type-safe, and efficient way to build powerful server-side applications. It simplifies API creation, database integration, and communication — all while keeping your code fast, organized, and easy to maintain.

Basic Dart
As tradition dictates “Hello, World!” is the first program you write when learning a new programming language and here is how you go about it.

1. Hello World
Every app requires the top-level function, where execution starts. Functions that don't explicitly return a value have the void return type. To display text on the console, you can use the top-level print() function.

2. Variables in Dart
Variables store data that can be used, changed, and referenced throughout a program. In Dart, you declare variables using the var, final, or const keywords, depending on whether the value can change or not.

It is also important to note that variables are declared similarly to JavaScript following the Carmel-case naming convention.

a. var
the variable is used when the value can change. The image below demonstrates its use case.


b. final
final is a variable used when the value is set once and cannot be reassigned.


c. const
The const variable is almost similar to the final variable declaration in that they both can not be reassigned, although the value in a const must be known and fixed when the program is compiled, not during execution. It’s ideal for values that will never change and are shared across the program, improving performance and memory efficiency.

It is also important to note that variables in dart can be declared based on their data types to ensure type safety and clarity in your code. You can explicitly specify the type, such as int, double, String, bool, or List, to indicate the kind of data the variable will store, we will get into depth on this when we look at data types.

Data types in dart
Data types in Dart define the kind of values a variable can hold and how those values are used in a program. They ensure that data is stored and processed correctly. Common data types in Dart include numbers, strings, booleans, lists, maps, and dynamic types each serving a unique purpose in representing and managing different kinds of information within your code.

a. Numbers
Numbers in Dart are used to represent numeric values. They come in two main types; int for whole numbers and double for decimal values.


An integer can be converted from an integer to a double as illustrated below:

b. Strings
Strings store text data and are enclosed in either single or double quotes. They can also include variables using string interpolation (e.g., "Hello $name").

c. Booleans
Booleans represent logical values and can only be true or false.

d. List
Lists are ordered collections of items, similar to arrays in other languages. They allow you to store multiple values in a single variable and access them by index.

Lists in Dart are one of the most powerful and flexible data structures, allowing you to store and manipulate ordered collections of items. Dart lists are zero-indexed, meaning the first element starts at index 0, and you can access or update values using their index positions. You can add elements with methods like .add(), .addAll(), .insert(), or .insertAll(), and remove them using .remove(), .removeAt(), .removeLast(), or .clear(). Lists can also be searched using .contains() and .indexOf(), and organized using .sort(), .shuffle(), or .reversed. Dart makes it easy to loop through lists with for loops or .forEach(). Overall, Dart lists are incredibly versatile perfect for storing, modifying, and processing data efficiently in any program.

e. Maps
Maps store data as key-value pairs, allowing quick lookups by key.

You can add, modify, or remove entries using methods like .addAll(), .remove(), and .clear(). Maps also let you view all keys and values, check for specific entries, and efficiently manage structured data.

f. Sets
Sets in Dart are unordered collections of unique items, meaning no duplicates are allowed. They’re useful when you need to store distinct values, like IDs or names.

it is also important to note we can use the set constructor to change a list to a set

g. Dynamic
A dynamic variable can hold values of any type, and its type can change at runtime. While it gives flexibility, it also removes compile-time type safety so it’s best used only when the data type isn’t known in advance.

Operators in dart
Now that we have looked at data types in dart lets dig on some of the operators used in dart. Operators are special symbols used to perform operations on variables and values. They make it easier to write expressions for tasks like arithmetic, comparisons, and logic. Common types include arithmetic operators (+, -, *, /), comparison operators (==, !=, <, >), logical operators (&&, ||, !), and assignment operators (=, +=, -=).

Control flow

1. Loops in Dart

a. For Loop
Repeats a block of code a fixed number of times.

b. For-in Loop
Used to iterate through elements in a collection like a list.

c. While Loop
Executes while the condition is true (checked before the loop runs).

d. Do-While Loop
Runs the code at least once before checking the condition.


e. Break Statement
Stops the loop immediately when a condition is met.

f. Continue Statement
Skips the current iteration and moves to the next one.

*2. Conditional statements

a. if Statement
Used to test if a condition is true before executing a block of code.

b. else Statement
Runs when the if condition is not met. As illustrated by the image above

c. else if Statement
Used to check multiple conditions one after another.

d. if-case Statement
Used for pattern matching in Dart (checks data structure patterns).

e. switch Statement
Simplifies handling of multiple possible values for a variable.

Conclusion.

I’ve just started learning Dart, and I decided to document and publish my progress both to help upcoming developers exploring the language and to track my own growth. So far, it’s been an exciting journey. I’m already amazed by what it can do. I can’t wait to start building amazing projects with this tool, with the ultimate goal of using technology to solve real-world problems. Happy coding.

You can follow along with my Dart learning journey and projects on GitHub

Top comments (0)