Introduction to Dart – Basic Syntax and Data Types

Dart is a programming language developed by Google that serves as the foundation for the Flutter framework. Its syntax differs significantly from many other languages, so it may take some time to get accustomed to. In this post, we’ll explore the basic syntax and data types of Dart before diving into Flutter development.

1. Introduction to Dart and Installation

Dart is a client-optimized, object-oriented, class-based language. While it can be used for web, server, and mobile application development, it’s primarily used for cross-platform mobile app development through Flutter.

Installing the Dart SDK:

  1. Download the SDK for your operating system from the official Dart website.
  2. Run the installer and follow the instructions.
  3. Verify the installation by running dart --version in your terminal.

2. Variables and Basic Data Types

Let’s look at how to declare variables in Dart and explore its basic data types.

Variable Declaration:

// Using var keyword for type inference
var name = 'John';

// Explicit type declaration
String lastName = 'Doe';

// final keyword (can only be assigned once)
final int age = 30;

// const keyword (compile-time constant)
const double PI = 3.14159;

Basic Data Types:

  1. Numbers
  • int: integers
  • double: floating-point numbers
int count = 3;
double price = 19.99;
  1. Strings
String greeting = 'Hello, Dart!';
String multiLine = '''
This is a
multi-line string.
''';
  1. Booleans
bool isReady = true;
  1. Lists
List<int> numbers = [1, 2, 3, 4, 5];
var fruits = ['apple', 'banana', 'orange'];
  1. Maps
Map<String, int> ages = {
  'John': 30,
  'Jane': 25,
};

3. Operators

Dart provides various operators for different operations.

Arithmetic Operators:

int a = 10;
int b = 3;

print(a + b);  // Addition: 13
print(a - b);  // Subtraction: 7
print(a * b);  // Multiplication: 30
print(a / b);  // Division: 3.3333333333333335
print(a ~/ b); // Integer division: 3
print(a % b);  // Modulus: 1

Comparison Operators:

print(a == b);  // Equal to: false
print(a != b);  // Not equal to: true
print(a > b);   // Greater than: true
print(a < b);   // Less than: false
print(a >= b);  // Greater than or equal to: true
print(a <= b);  // Less than or equal to: false

Logical Operators:

bool x = true;
bool y = false;

print(x && y);  // AND: false
print(x || y);  // OR: true
print(!x);      // NOT: false

4. Control Flow

Let’s look at the basic constructs for managing control flow in Dart.

if-else Statement:

int number = 5;

if (number > 0) {
  print('Positive number');
} else if (number < 0) {
  print('Negative number');
} else {
  print('Zero');
}

for Loop:

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

// for-in loop
var numbers = [1, 2, 3, 4, 5];
for (var number in numbers) {
  print(number);
}

while and do-while Loops:

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

do {
  print(count);
  count--;
} while (count > 0);

switch-case Statement:

String grade = 'B';

switch (grade) {
  case 'A':
    print('Excellent');
    break;
  case 'B':
    print('Good');
    break;
  case 'C':
    print('Fair');
    break;
  default:
    print('Poor');
}

Conclusion

In this post, we’ve covered the basic syntax and data types of the Dart language. In the next post, we’ll explore functions and object-oriented programming basics in Dart.

If you have any questions about the Dart language, feel free to leave a comment!

Related Resources

Official Dart website: Dart programming language | Dart

Official Flutter website: Flutter – Build apps for any screen

Leave a Reply

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