Dart Basic- Syntax

Home /

Table of Contents

Syntax of Dart Programming

Syntax refers to the set of rules that govern how programs are written. Every programming language has its own syntax. The syntax of Dart programming language includes the following elements:

Variables and Operators

Dart supports a wide range of data types including numbers, strings, lists, and maps. Operators such as arithmetic, logical, and bitwise operators are used to perform operations on these data types.

void main() {
  int x = 10;
  double y = 3.14;
  String name = "John Doe";
  List<int> numbers = [1, 2, 3, 4];
  Map<String, dynamic> person = {"name": "John", "age": 30};

  // Arithmetic operators
  int sum = x + 5;
  int difference = x - 5;
  double product = y * 2;
  double quotient = y / 2;

  // Logical operators
  bool isTrue = true;
  bool isFalse = false;
  bool andResult = isTrue && isFalse; // false
  bool orResult = isTrue || isFalse; // true

  // Bitwise operators
  int a = 60; // 0011 1100
  int b = 13; // 0000 1101
  // int andResult = a & b; // 0000 1100
  // int orResult = a | b; // 0011 1101
  int xorResult = a ^ b; // 0011 0001

}

Classes

Dart is an object-oriented language and supports classes and objects. Classes define the structure and behavior of objects, while objects represent instances of these classes.

class Person {
  String name;
  int age;

  void sayHello() {
    print("Hello, my name is $name and I am $age years old.");
  }
}

Person john = new Person();
john.name = "John";
john.age = 30;
john.sayHello();

Functions

Dart supports functions, which are blocks of code that can be called by other parts of the program.

int sum(int a, int b) {
  return a + b;
}

void main() {
  int result = sum(5, 10);
  print(result);
}

Expressions and Programming Constructs

Dart includes expressions and programming constructs such as conditional statements (if/else), loops (for/while), and switch statements.

int x = 10;

if (x > 5) {
  print("x is greater than 5");
} else {
  print("x is less than or equal to 5");
}

for (int i = 0; i < 5; i++) {
  print(i);
}

int i = 0;
while (i < 5) {
  print(i);
  i++;
}

switch (x) {
  case 0:
    print("x is 0");
    break;
  case 1:
    print("x is 1");
    break;
  default:
    print("x is neither 0 nor 1");
    break;
}

Comments

Comments are used to add descriptions and explanations to code. In Dart, comments can be single-line (//) or multi-line (/* */).

// This is a single-line comment

/*
This is a
multi-line
comment
*/

Libraries and Packages

Dart provides support for libraries and packages, which help in organizing code and making it reusable.

// Importing a library
import 'dart:math';

void main() {
  double number = 3.14;
  double squareRoot = sqrt(number);
  print(squareRoot);
}

// Creating a package
// pubspec.yaml
name: my_package
description: My first Dart package
version: 1.0.0

// lib/my_package.dart
library my_package;

int sum(int a, int b) {
  return a + b;
}

Typedefs

Typedefs are used to define function types.

typedef MyFunction = void Function(int);

void printNumber(int number) {
  print(number);
}

void main() {
  MyFunction f = printNumber;
  f(5);
}

Collections / Generics

Dart provides a variety of data structures such as arrays, lists, and maps. These data structures can also be used with generics to create more flexible and reusable code.

class MyGenericClass<T> {
  T value = null;

  void setValue(T value) {
    this.value = value;
  }

  T getValue() {
    return value;
  }
}

void main() {
  List<String> names = ["Alice", "Bob", "Charlie"];
  print(names.length);

  Map<String, int> scores = {"Alice": 90, "Bob": 80, "Charlie": 70};
  print(scores["Alice"]);

  MyGenericClass<String> myStringClass = MyGenericClass<String>();
  myStringClass.setValue("Hello, World!");
  print(myStringClass.getValue());
}

Execution of a Dart Program

Dart programs can be executed in two ways: via the terminal or via an IDE such as WebStorm. To execute a Dart program via the terminal, navigate to the directory where the program is saved and type the command dart file_name.dart. To execute a Dart program via WebStorm, right-click on the Dart script file and select “Run .”

Dart programs can be run in two modes: checked mode and production mode. Checked mode is recommended during development and testing, as it enforces various checks such as type-checking, which helps in debugging. To turn on the checked mode, use the -c or –checked option before the script-file name while running the script. However, for improved performance, it is recommended to run the script in production mode.

Identifiers in Dart are names given to program elements such as functions, variables, and arrays. Identifiers cannot begin with a digit, include special symbols except for underscore (_) or dollar sign ($), or be keywords. They must be unique and are case-sensitive. Identifiers cannot contain spaces.

In summary, understanding the syntax of Dart is essential for writing effective and efficient programs. In addition to the basic elements of syntax, Dart also provides support for libraries, packages, and data structures, making it a powerful language for building web, mobile, and desktop applications.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit
Other Recommended Article