Dart Programming Introduction -Dart Language Fundamentals Guide

Haris Bin Nasir Avatar

·

·

Dart is a versatile and powerful programming language primarily used for building mobile, desktop, server, and web applications. This post will cover the fundamentals of Dart programming, including variables, data types, strings, numbers, functions, decision-making, loops, classes & objects, maps, lists, futures, async/await, and null safety. Each section provides code examples to help you understand the basics. For more detailed explanations, see our detailed articles on each topic.

Dart Variables

Variables in Dart are used to store data. Dart is a statically-typed language, meaning you need to specify the type of data a variable will hold.

void main() {
  String name = 'David';
  int age = 30;
}

For further details, see our detailed article on Dart Variables.

Dart Data Types

Dart supports a variety of data types, including int, double, String, bool, List, and Map. Each data type serves a specific purpose.

Strings

Strings represent a sequence of characters. We declare them by using single or double quotes.

void main() {
  String greeting = 'Hello, World!';
  print(greeting);
}
Numbers

Dart has two types of numbers: int for integers and double for floating-point numbers.

void main() {
  int year = 2023;
  double pi = 3.14159;
}

For more information, see our detailed article on Dart Data Types.

Dart Functions

Functions are blocks of code that perform a specific task. They help in organizing code into reusable blocks.

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

void main() {
  greet('David');
}

For further details, see our detailed article on Dart Functions.

Dart Decision Making

Decision-making structures like if, else, and switch are used to execute code based on conditions.

void main() {
  int age = 18;

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

For more complex scenarios, see our detailed article on Dart Decision Making.

Dart Loops

Loops in Dart are used to execute a block of code repeatedly as long as a specified condition is met. Dart supports several types of loops, including for, while, and do-while.

For Loop

The for loop is commonly used when the number of iterations is known. It runs a block of code a specified number of times.

void main() {
  for (int i = 0; i < 5; i++) {
    print('Iteration $i');
  }
}

While Loop

The while loop is used when the number of iterations is not known beforehand. It executes the block of code as long as the condition is true.

void main() {
  int i = 0;
  while (i < 5) {
    print('Iteration $i');
    i++;
  }
}

For a deeper dive into loops, see our detailed articles on Dart Loops.

Dart Classes & Objects

Dart is an object-oriented programming language, meaning it uses classes and objects to structure code.

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 person = Person('David', 30);
  person.introduce();
}

For more examples and explanations, see our detailed article on Dart Classes & Objects.

Dart Maps

Maps in Dart are collections of key-value pairs. We Use them to associate data with specific keys.

void main() {
  Map<String, int> phonebook = {'David': 1234, 'Mary': 5678};
  print(phonebook['Mary']);
}

For further reading, see our detailed article on Dart Maps.

Dart Lists

Lists, order collections of objects, are similar to arrays in other programming languages.

void main() {
  List<String> fruits = ['Apple', 'Banana', 'Mango'];
  print(fruits[0]);
}

For more information, see our detailed article on Dart Lists.

Dart Future

The Future class in Dart is used to represent a potential value or error that will be available at some time in the future.

Future<String> fetchData() async {
  return 'Data loaded';
}

void main() {
  fetchData().then((data) => print(data));
}

Async / Await Dart

Dart provides async and await keywords to work with asynchronous code, making it easier to read and write.

Future<void> fetchData() async {
  await Future.delayed(Duration(seconds: 2));
  print('Data loaded');
}

void main() {
  fetchData();
}

Null Safety In Dart

Null safety in Dart ensures that no variable can contain a null value unless you explicitly allow it.

void main() {
  String? name = null;
  print(name); // This is safe because of the ? operator
}

To explore more, see our detailed article on Null Safety in Dart.


Conclusion

Dart is a powerful language that offers a solid foundation for developing robust applications across multiple platforms. This post covered the fundamentals of Dart programming, including variables, data types, functions, decision-making structures, loops, and object-oriented principles like classes and objects. Additionally, we explored advanced features such as futures, async/await, and null safety. Each of these topics is essential for mastering Dart and is crucial for any developer looking to build Flutter applications.

For those seeking to deepen their understanding, be sure to explore our detailed articles on each topic. These resources provide in-depth explanations, advanced examples, and practical insights to help you become proficient in Dart programming.

Happy Coding…!!!

Leave a Reply

Your email address will not be published. Required fields are marked *