Skip to main content

Days 1-2: Dart Introduction and Development Tools

Updated 3 min readMarkdown
Part of seriesExploring Dart, Flutter, Serverpod for Stunning Apps
Days 1-2: Dart Introduction and Development Tools

Objective:

  • Install Dart SDK and development tools (VSCode/IntelliJ).

  • Learn basic Dart syntax: variables, data types, operators.


1. Dart SDK and Development Tools Installation

Dart SDK Installation:

  1. Download Dart SDK:

    • Visit the Dart SDK Download Page.

    • Select the appropriate SDK for your operating system (Windows, macOS, Linux).

  2. Install Dart SDK:

    • Follow the installation instructions for your operating system.
  3. Verify Installation:

    • Open your terminal or command prompt.

    • Run the command dart --version to check if Dart is installed correctly.

Development Tools Installation:

  1. Visual Studio Code (VSCode):

    • Download and install Visual Studio Code.

    • Open VSCode and go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or pressing Ctrl+Shift+X.

    • Search for the Dart extension and install it.

  2. IntelliJ IDEA:

    • Download and install IntelliJ IDEA.

    • Open IntelliJ IDEA and go to File > Settings > Plugins.

    • Search for the Dart plugin and install it.


2. Basic Dart Syntax

Variables:

Variables in Dart can be declared using var, final, or const.

void main() {
  var name = 'John';  // A variable that can be reassigned
  final age = 25;     // A variable that cannot be reassigned
  const pi = 3.14;    // A compile-time constant

  print('Name: $name, Age: $age, Pi: $pi');
}

Data Types:

Dart is a strongly typed language, and it supports various data types like int, double, String, bool, List, and Map.

void main() {
  int a = 10;
  double b = 20.5;
  String c = 'Hello';
  bool d = true;
  List<int> e = [1, 2, 3];
  Map<String, int> f = {'one': 1, 'two': 2};

  print('Int: $a, Double: $b, String: $c, Bool: $d, List: $e, Map: $f');
}

Operators:

Dart supports various operators, including arithmetic, relational, logical, and assignment operators.

void main() {
  int a = 10;
  int b = 20;

  // Arithmetic Operators
  print('Addition: ${a + b}');
  print('Subtraction: ${a - b}');
  print('Multiplication: ${a * b}');
  print('Division: ${a / b}');
  print('Modulus: ${a % b}');

  // Relational Operators
  print('Equal: ${a == b}');
  print('Not Equal: ${a != b}');
  print('Greater than: ${a > b}');
  print('Less than: ${a < b}');

  // Logical Operators
  bool c = true;
  bool d = false;
  print('AND: ${c && d}');
  print('OR: ${c || d}');
  print('NOT: ${!c}');

  // Assignment Operators
  int e = 5;
  e += 5; // e = e + 5
  print('Assignment: $e');
}

Examples with Explanations

Example 1: Basic Dart Program

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

Explanation: This is the simplest Dart program. The main() function is the entry point of the program, and print() is used to output text to the console.

Example 2: Using Variables and Data Types

void main() {
  var name = 'Alice';
  int age = 30;
  double height = 5.6;
  bool isStudent = false;

  print('Name: $name');
  print('Age: $age');
  print('Height: $height');
  print('Is Student: $isStudent');
}

Explanation: This program demonstrates declaring variables of different data types and printing their values.

Example 3: Using Operators

void main() {
  int a = 15;
  int b = 4;

  print('Sum: ${a + b}');
  print('Difference: ${a - b}');
  print('Product: ${a * b}');
  print('Quotient: ${a / b}');
  print('Remainder: ${a % b}');
}

Explanation: This program uses arithmetic operators to perform basic mathematical operations on two integers.


Visual Explanations

1. Dart SDK Installation:

  • Figure 1: Dart SDK download page.

  • Figure 2: Verifying Dart installation in terminal.

2. Variables and Data Types:

  • Figure 3: Example of declaring variables and data types in Dart.

3. Operators:

  • Figure 4: Example of using operators in Dart.

By following this guide, you'll have a solid understanding of the basics of Dart and be ready to move on to more advanced concepts in the following days. Happy coding!

About the author

No comments yet. Be the first to comment.

More from this blog