DART Coding

Building Cross-Platform Greatness With DART

At Codelabpro, we make coding easier and enable learners with top-notch programming tutorials, courses, and resources. Whether you are a novice making your initial strides in coding or a seasoned developer aiming to improve your skills, we offer practical, real-world insights to assist you in achieving success.

DART

Dart is an object-oriented, class-based, garbage-collected language featuring strong typing, null safety, and just-in-time (JIT) as well as ahead-of-time (AOT) compilation. Its design focuses on enhancing developer productivity and it is especially effective for creating user interfaces.

Key Features:
  • Strong typing: Guarantees that variables are assigned specific types. 
  • Null Safety: Aids in preventing runtime errors by identifying null-related issues during compile time. 
  • Cross-Platform Support: Allows for writing code once and executing it on various platforms. 
  • Asynchronous Programming: Incorporates elements like async and await for improved management of asynchronous operations.
Example Code: Hello World in Dart
void main() { 
print(‘Hello, World!’);
 }
 
Explanation
void main() serves as the starting point for a Dart application. print() is a built-in function used to show output in the console.

Explanation of Core Concepts

1. Variables 

Variables hold data. Dart accommodates various data types: int, double, String, bool, and others.

Explanation

 Dart utilizes type inference with var, yet you have the option to define types clearly. $variable is utilized for string interpolation.

void main() {

int age = 25;

double height = 5.9;

String name = ‘John’;

bool isActive = true;

print(‘Name: $name, Age: $age, Height: $height, Active: $isActive’);

}

2. Functions

 Functions allow you to encapsulate logic.

Explanation

Dart allows both named and positional arguments. Functions can return values (return) or perform an action (void).

int addNumbers(int a, int b) {

  return a + b;}

void main() {

  int result = addNumbers(5, 3);

  print(‘Sum: $result’);}

3. Control Flow 

Dart supports conditional statements and loops.

Explanation

  • if-else checks conditions. 
  • Loops (for, while, do-while) are used for repeated execution.

void main() {

  int number = 10;

  if (number % 2 == 0) {

    print(‘$number is even’);

  } else {

    print(‘$number is odd’);}

  for (int i = 1; i <= 5; i++) {

    print(‘Count: $i’);

  }}

4. Classes and Objects 

Dart is object-oriented, meaning you can use classes to define blueprints for objects.

Explanation

  •  class defines a blueprint.
  •  this keyword initializes class variables. 

Objects are instances of classes.

OOP is a powerful programming paradigm that structures software design around data, or objects, rather than functions and logic.

class Person {

  String name;

  int age;

  Person(this.name, this.age);

  void introduce() {

    print(‘Hi, I am $name and I am $age years old.’);

  }}

void main() {

  Person p = Person(‘Alice’, 30);

  p.introduce();

}

5. Asynchronous Programming 

Dart handles asynchronous operations using Future, async, and await.

Explanation

  • Future represents a value that will be available later. 
  • await pauses execution until the future is complete.

Future<String> fetchData() async {

  return Future.delayed(Duration(seconds: 2), 

  () => ‘Data fetched!’);}

void main() async {

  print(‘Fetching…’);

  String data = await fetchData();

  print(data);

}

6. Null Safety 

Dart’s null safety ensures that variables cannot have null values unless explicitly declared.

Explanation

Use ? to declare nullable variables.

Null safety, referred to as void safety, is a programming principle that avoids mistakes caused by referencing variables that are assigned a null value. It is a characteristic that aids in enhancing code security and consistency by identifying null errors during the development phase, instead of at runtime.

void main() {

  int? nullableNumber; // Can be null

  nullableNumber = 5;

  int nonNullableNumber = 10; // Cannot be null

  print(nullableNumber);

  print(nonNullableNumber);

}

 
Use Cases 
  • Mobile Development: Flutter apps. 
  • Web Development: Client-side scripting with Dart. 
  • Server Applications: Backend services using frameworks like Aqueduct. 
  • Desktop Applications: Cross-platform apps.
  •