fbpx
Dart Language Syntax

Dart Programming Language Syntax

The Dart programming language has a C-style syntax that is inspired by languages like C++, Java, C#, and JavaScript. Dart programs are made up of a series of statements, each ending with a semicolon (;). The => expr syntax is a shorthand for { return expr; } and is sometimes called arrow syntax. Only an expression, not a statement, can appear between the arrow (=>) and the semicolon (;). If you’re diving into the world of Flutter development, understanding the Dart programming language is essential. Dart serves as the backbone of Flutter, providing a concise and powerful syntax for building mobile, web, and desktop applications. In this tutorial, we’ll explore the fundamentals of Dart syntax, covering key concepts and providing code snippets to illustrate each point.

What is Dart?

Dart is an open-source, object-oriented programming language developed by Google. It’s known for its simplicity, speed, and versatility, making it an ideal choice for building Flutter applications. Dart offers features such as strong typing, asynchronous programming, and a flexible syntax that empowers developers to create high-performance applications with ease.

Dart Syntax Basics

Let’s start with the basics of Dart syntax:

Variables and Data Types

In Dart, you declare variables using the var, final, or const keywords. Dart is statically typed, meaning you can explicitly declare the data type of a variable, or let Dart infer it for you.

// Variable declaration
var name = 'Usama'; // Usama
final age = 30; // 30
const PI = 3.14; // 3.14

// Data types
String message = 'Hello, Dart!'; // Hello, Dart!
int count = 10; // 10
double price = 19.99; // 19.99
bool isActive = true; // true

Functions

Functions in Dart are declared using the void keyword for functions that don’t return a value, and you can specify the return type for functions that do return a value.

// Function declaration
void greet() {
  print('Hello, World!'); // Hello, World!
}

String greetUser(String name) {
  return 'Hello, $name!'; // Hello, Alice!
}

Control Flow

Dart supports traditional control flow statements like if, else, switch, for, and while.

// Conditional statements
if (age >= 18) {
  print('You are an adult.'); // You are an adult.
} else {
  print('You are a minor.'); // You are a minor.
}

// Looping
for (int i = 0; i < 5; i++) {
  print('Count: $i'); 
  // Count: 0
  // Count: 1
  // Count: 2
  // Count: 3
  // Count: 4
}

Classes and Objects

Dart is an object-oriented language, and classes are the building blocks of Dart programs.

// Class declaration
class Person {
  String name;
  int age;

  // Constructor
  Person(this.name, this.age);

  // Method
  void displayInfo() {
    print('Name: $name, Age: $age');
  }
}

// Object instantiation
var person = Person('Alice', 25);
person.displayInfo(); // Name: Alice, Age: 25

Try it Yourself in DartPad!

Now that you’ve learned about Dart syntax basics, why not try out some code snippets in DartPad? DartPad provides an online editor where you can write and run Dart code directly in your browser. Simply visit DartPad and start experimenting with Dart syntax to solidify your understanding.

In conclusion, mastering Dart syntax is essential for becoming proficient in Flutter development. By grasping the concepts covered in this tutorial and practicing with DartPad, you’ll be well on your way to building stunning Flutter applications. Happy coding!

About the Author

Usama Sarwar

Meet Usama Sarwar, a leading technology expert, open-source contributor, and mentor, dedicated to leaving a positive mark in the tech industry. With a wide-ranging expertise spanning graphic design, digital marketing, cross-platform app development, and eCommerce solutions, Usama has made waves in sectors like health tech, fintech, and e-commerce. He's not just an influencer, but an inspiration, proving that the right attitude and approach can turn dreams into reality. Connect with Usama Sarwar today to embark on your journey to a brighter tech-focused future.

Recommended articles