Skip to main content

Command Palette

Search for a command to run...

Understanding Dart: Basic Syntax, Variables, and Data Types for Beginners

Updated
4 min read

Welcome to the world of Dart programming! Whether you're starting from scratch or brushing up on your skills, this blog post will guide you through the basic syntax, variables, and data types in Dart. These concepts are fundamental to becoming proficient in Dart, and we’ll use real-life examples to help make these ideas more practical and relatable.


What is Dart?

Dart is a modern programming language primarily used for developing cross-platform mobile applications, especially with Flutter. It’s also a great choice for web and server-side applications. Dart’s simple and readable syntax makes it ideal for beginners, yet it’s powerful enough for professional developers.


1. Basic Syntax in Dart

Let’s start by exploring how a Dart program is structured. Dart uses a C-style syntax, which means it shares similarities with languages like JavaScript and Java.

A Simple Example

Here’s a simple Dart program that prints a message to the console:

void main() {
  print('Hello, Dart!');
}

2. Variables in Dart

Variables are used to store values that can be used later. Dart allows you to define variables in different ways. You can either let Dart infer the type of a variable or explicitly declare the type.

Declaring a Variable

You can declare a variable with var, which allows Dart to automatically infer the type of the variable, or you can specify the type explicitly.

void main() {
  var userName = 'Alice'; // Dart infers this as a String
  String cityName = 'New York'; // Explicitly declaring a String

  print(userName);
  print(cityName);
}

Output:
Alice

New York!

Changing the Value of a Variable

Once a variable is declared, its value can be changed (unless it’s declared with final or const).

void main() {
  var balance = 1000.0; // Starting balance
  print('Initial Balance: \$${balance}');

  balance = 1200.50; // Updated balance
  print('Updated Balance: \$${balance}');
}

3. Data Types in Dart

Dart provides several built-in data types, such as Numbers, Strings, and Booleans. Let’s explore these data types with practical examples.

Numbers

Dart supports two types of numbers:

  • int: Integer values (whole numbers).

  • double: Decimal values (floating-point numbers).

void main() {
  int userAge = 30; // Integer: Age of the user
  double orderAmount = 19.99; // Double: Price of a product

  print('User Age: $userAge');
  print('Order Amount: \$${orderAmount}');
}

Strings

Strings represent text and can be enclosed in either single (') or double (") quotes.

void main() {
  String productName = 'Wireless Headphones';
  String customerName = 'Alice';

  print('Customer: $customerName');
  print('Product: $productName');
}

String Interpolation

String interpolation allows you to embed variables or expressions inside a string using the $ symbol.

void main() {
  String productName = 'Laptop';
  double productPrice = 999.99;

  print('The price of the $productName is \$${productPrice}');
}

Booleans

Booleans store true or false values and are essential for making decisions in programs.

void main() {
  bool isAuthenticated = true;
  bool hasDiscount = false;

  if (isAuthenticated) {
    print('Welcome, authenticated user!');
  } else {
    print('Please log in first.');
  }

  print('Discount Applied: $hasDiscount');
}

Summary Table of Dart Data Types

Data TypeExampleUse Case
intint count = 5;Counting items, age, scores.
doubledouble price = 9.99;Storing prices, percentages, measurements.
StringString name = 'Alice';Storing names, messages, textual data.
boolbool isActive = true;Flags, conditions, authentication status.
ListList<int> nums = [1, 2, 3];Collections of ordered data.
MapMap<String, String> data = {};Key-value pairs like dictionaries.
Runes'\u{1F600}'Emojis, special characters.
SymbolSymbol lib = #example;Advanced reflection or metadata.

Conclusion

To wrap things up, we’ve learned the fundamentals of Dart syntax, variables, and data types. These basics are the foundation for building powerful applications with Dart and Flutter.

We also explored real-life examples, such as:

  • Managing a user profile in an app.

  • Calculating a bank balance in a banking app.

  • Handling product information and user authentication in an e-commerce platform.

The beauty of Dart is its simplicity, making it accessible for beginners, while still offering advanced features for more complex projects. As you dive deeper into Dart, you’ll be able to create more dynamic and interactive applications for any platform.

Happy coding! If you have any questions or need more examples, feel free to leave a comment below!

More from this blog

S

Suneel D.K

25 posts

Learn Flutter and Dart with easy tutorials and YouTube videos! Master app development with tips, tricks, and detailed guides for beginners and pros alike. Start building today!