Dart Basic-Easy way to learn Dart

Home /

Table of Contents

Introduction to Dart

Dart is an object-oriented, class-based programming language that is designed for building web, mobile, and desktop applications. It is developed by Google and is often used with the Flutter framework for building mobile applications.

Dart is a programming language optimized for mobile apps. It is originally developed by Google and later approved as a standard by ECMA. Dart is a new programming language meant for the server as well as the browser. Introduced by Google, the Dart SDK ships with its compiler – the Dart VM. The SDK also includes a utility -dart2js, a transpiler that generates the JavaScript equivalent of a Dart Script. This tutorial provides a basic level of understanding of the Dart programming language.

This tutorial assumes that the readers have adequate knowledge of object-oriented programming languages.

The articles in this Dart Basic series are listed below.

Why should we learn Dart?

Dart offers ahead-of-time compilations, which automatically compile code into a format that a native machine can easily read, and allows an application to natively execute binary files. By compiling code during the build process, programmers can also achieve faster UI rendering in browsers.

Dart is a general-purpose programming language that can be used to build a wide range of applications. Some of the main use cases and applications of Dart are:

  • Web Development: Dart can be used to build web applications, including single-page applications (SPAs), through the use of frameworks such as AngularDart, which is built using the Dart language.
  • Mobile Development: Dart can also be used to build native mobile applications for both Android and iOS platforms through the use of Google’s Flutter framework. Flutter uses Dart as its programming language and allows developers to create high-performance, visually rich, and natively compiled mobile apps.
  • Desktop Development: Dart can be used to build desktop applications using the Flutter framework, which provides widgets and tools that allow developers to build beautiful, responsive desktop apps.
  • Server-Side Development: Dart can be used to build server-side applications through the use of frameworks such as Aqueduct, which provides a complete set of tools for building scalable and maintainable server-side applications.
  • Game Development: Dart can be used to build 2D and 3D games through the use of game engines such as Flame and StageXL, which are built using Dart.

Quick Intro of Dart

Install Dart Before you can start programming in Dart, you’ll need to install it (However, you will find an article about installing dart in this article series). You can download Dart SDK from the official Dart website. Then, create a “Hello, World!” program: Once you have Dart installed, create a new file called “hello_world.dart” and enter the following code:

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

Save the file and run it using the Dart command-line tool by typing dart hello_world.dart. You should see the message “Hello, World!” printed to the console.

Variables and Data Types

In Dart, you can declare variables using the var keyword. Dart is a statically-typed language, which means that all variables must have a specified data type. Dart supports a variety of data types, including numbers, strings, booleans, and lists. You can declare a variable in Dart using the var keyword or by specifying its type explicitly.

For example:

var message = 'Hello, Dart!';

Dart has several built-in data types, including int, double, String, bool, and List. You can use these data types to declare variables:

int age = 42;
double pi = 3.14;
String name = 'Alice';
bool isDartAwesome = true;
List fruits = ['apple', 'banana', 'cherry'];

Control structures allow you to control the flow of your program. Dart has several control structures that allow you to control the flow of your program. These include if statements, for loops, while loops, and switch statements:

if statement

if (isDartAwesome) {
print('Dart is awesome!');
} else {
print('Dart is not awesome :(');
}

Loops

for (var fruit in fruits) {
print(fruit);
}
while (age < 50) {
age++;
}

switch case Statement

switch (name) {
case 'Alice':
print('Hi, Alice!');
break;
case 'Bob':
print('Hi, Bob!');
break;
default:
print('Hello, stranger!');
}

Functions

Functions allow you to reuse code and make your programs more modular. In Dart, you can define functions using the void keyword for functions that don’t return a value, or a specific return type for functions that do:

void sayHello(String name) {
print('Hello, $name!');
}

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

You can call functions like this:

sayHello('Alice'); // prints "Hello, Alice!"
int sum = add(2, 3); // sum is now 5

Class and Object

Dart is an object-oriented language, which means that you can define classes and create objects from those classes. A class is a blueprint for an object, and an object is an instance of a class.

class Person {
  String name;
  int age;

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

  void celebrateBirthday() {
  age++;
  print('Happy birthday, $name! You are now $age years old.');
  }
}

Person alice = Person(); // create a new Person object
alice.name = 'Alice';
alice.age = 25;
alice.sayHello(); // prints "Hello, my name is Alice and I am 25 years old."
alice.celebrateBirthday(); // prints "Happy birthday, Alice! You are now 26 years old."

Inheritance

Inheritance is a fundamental concept in object-oriented programming that allows you to create a new class based on an existing class. The new class inherits all the properties and methods of the existing class, and can also override or add new ones.

class Student extends Person {
    String major;

    void study() {
        print('$name is studying $major.');
    }

    @override
    void sayHello() {
        print('Hello, my name is $name and I am $age years old. I am majoring in $major.');
    }
}

Student bob = Student(); // create a new Student object
bob.name = 'Bob';
bob.age = 20;
bob.major = 'Computer Science';
bob.sayHello(); // prints "Hello, my name is Bob and I am 20 years old. I am majoring in Computer Science."
bob.study(); // prints "Bob is studying Computer Science."

Asynchronous programming

Asynchronous programming is an important concept in modern web and mobile development, as it allows applications to perform tasks without blocking the user interface. Dart supports asynchronous programming through the use of async and await keywords, as well as the Future and Stream classes.

Future getNumber() async {
    await Future.delayed(Duration(seconds: 1)); // simulate a delay
    return 42;
}
void main() async {
    print('Getting number…');
    int number = await getNumber();
    print('Got number: $number');
}

This program will output:

Getting number…
Got number: 42

Libraries

Dart allows you to organize your code into libraries, which can be imported and reused in other parts of your program. You can create a library by using the library keyword, and import other libraries using the import keyword.

library my_library;

import 'dart:math';

int getRandomNumber() {
    return Random().nextInt(100);
}

You can then use this library in another part of your program:

import 'my_library.dart';

void main() {
    int randomNumber = getRandomNumber();
    print('Random number: $randomNumber');
}

Dart is a powerful and modern programming language that is well-suited for building web, mobile, and desktop applications. With its strong typing system, support for asynchronous programming, and modern syntax, it offers a range of features that make it a popular choice among developers. By learning Dart, you can open up new possibilities for your programming career, and create high-performance, scalable applications that meet the needs of modern users.

That’s a brief introduction to Dart programming. There’s a lot more to learn and you can find the basics about Dart in this tutorial series, but this should give you a good starting point!

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

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article