Dart Language Basics

From Dart Wiki
Jump to navigation Jump to search

Dart Language Basics[edit]

Introduction[edit]

Dart is a modern, object-oriented programming language designed for building scalable web and mobile applications. It combines the best features of other popular programming languages, making it both powerful and flexible. This article provides an overview of the basics of Dart, including its syntax, data types, control flow, and more.

Syntax[edit]

Dart uses a C-style syntax that is familiar to many developers. Here are some key points to remember:

  • Statements are terminated with a semicolon (;).
  • Blocks of code are enclosed within curly braces ({ }).
  • Dart is a case-sensitive language.
  • Comments can be written using either single line (//) or multiline (/* */) syntax.

Data Types[edit]

Dart has a strong type system that allows for strict type checking. Here are the most common data types in Dart:

  • `int`: represents integers.
  • `double`: represents floating-point numbers.
  • `bool`: represents boolean values (true or false).
  • `String`: represents sequences of characters.
  • `List`: represents ordered collections of objects.
  • `Map`: represents key-value pairs.

Variables[edit]

In Dart, variables are used to store values that can be later accessed and manipulated. Here's how to declare and initialize variables:

```dart // Declaring variables int age;

// Declaring and initializing variables String name = "John Doe"; double pi = 3.14159;

// Variables can also be declared using the var keyword var count = 10; ```

Control Flow[edit]

Dart provides various control flow statements for making decisions and iterating over collections. Here are some commonly used control flow statements:

  • `if` statement: executes a block of code if a condition is true.
  • `switch` statement: selects one of many code blocks to execute.
  • `for` loop: iterates a block of code a specific number of times.
  • `while` loop: repeatedly executes a block of code while a condition is true.
  • `do-while` loop: repeatedly executes a block of code until a condition is false.

Functions[edit]

Functions in Dart are used to encapsulate reusable pieces of code. Here's an example of a simple function:

```dart // Declaring a function int multiply(int a, int b) {

 return a * b;

}

// Calling the function int result = multiply(5, 3); print(result); // Output: 15 ```

Object-Oriented Programming[edit]

Dart is an object-oriented language, which means it provides support for classes and objects. Here's an example of a class definition and object instantiation:

```dart // Declaring a class class Person {

 String name;
 int age;
 // Constructor
 Person(this.name, this.age);
 // Method
 void sayHello() {
   print("Hello, my name is $name and I am $age years old.");
 }

}

// Creating an object Person person = Person("John Doe", 25); person.sayHello(); // Output: Hello, my name is John Doe and I am 25 years old. ```

Conclusion[edit]

This article provided a high-level overview of the Dart programming language. It covered the basics of Dart syntax, data types, control flow, functions, and object-oriented programming. Armed with this knowledge, you are ready to dive deeper into the world of Dart and take advantage of its powerful features in your own projects.

For more detailed information and in-depth tutorials, please check out the following articles on our Dart Wiki:

  • [Link to Dart Tutorial]
  • [Link to Dart Best Practices]
  • [Link to Dart Troubleshooting]

We hope you find this resource helpful in your journey to master Dart!