Dart Basic — Part 1

Dart Basic — Part 1

Exploring Dart Fundamentals: Variables, Types, Constants, and Operators


Dart, with its simplicity and power, is a modern programming language that caters to various development needs, from mobile applications to server-side solutions. In this comprehensive guide, we’ll explore the foundational concepts of Dart through practical examples.

1. Variables and Types:

1.1 Variables: Variables store data that can be manipulated and referenced in a program. In Dart, you declare variables using the var, final, or const keywords.

  • var: Declares a variable whose type is inferred by the Dart compiler based on the assigned value. For example:
var age = 23; //age as an integer
  • final: final variables in Dart are variables whose values cannot be changed once they are initialized. They must be initialized before they are used, and once initialized, their values cannot be reassigned.

  • Final variables are initialized when they are first accessed, and their value remains constant throughout the program’s execution.

  • Final variables can be initialized with a value at the time of declaration or within constructors.

final name = 'Sadanand'; // name is assigned 'Sadanand' and it cannot be changed
  • const: const variables are compile-time constants in Dart. They are implicitly final but also compile-time constants. Their values must be known at compile-time.

  • Const variables are evaluated and set at compile-time, not runtime.

  • They are useful for declaring values that will not change during the execution of the program.

const PI = 3.1415; // PI is a compile-time constant

1.2 Types: Dart is a statically typed language, meaning each variable has a specific data type known at compile-time. Dart provides several built-in data types:

  • Numbers: Dart supports both integers and floating-point numbers.
int age = 30;
double height = 5.11;
  • Strings: Used to represent textual data.
String name = 'Sadanand';
  • Booleans: Represents a true or false value.
bool isAdult = true;
  • Lists: Ordered collections of objects.
List<int> numbers = [1, 2, 3, 4, 5];
  • Maps: Unordered collections of key-value pairs.
Map<String, dynamic> person = {
    'name': 'Sadanand',
    'age': 23,
    'isAdult': true
};

2. Dynamic:

Dynamic: Represents a variable whose type can change dynamically at runtime.

dynamic dynamicVariable = 'Sadanand';
dynamicVariable = 23; // Now dynamicVariable is an integer

3. Common Operators:

Common operators in programming languages are symbols or keywords used to perform various operations on data. Here’s a brief explanation of common operators used in programming:

3.1 Arithmetic Operators:

  • +: Adds two numbers.

  • -: Subtracts the second number from the first.

  • *: Multiplies two numbers.

  • /: Divide the first number by the second.

  • ~/: Truncating Division returns an integer result by rounding towards zero.

  • %: Modulus operator returns the remainder of the division.

3.2 Relational Operators:

  • >: Checks if the first operand is greater than the second.

  • <: Checks if the first operand is less than the second.

  • >=: Checks if the first operand is greater than or equal to the second.

  • <=: Checks if the first operand is less than or equal to the second.

3.3 Equality Operators:

  • ==: Checks if two operands are equal.

  • !=: Checks if two operands are not equal.

3.4 Logical Operators:

  • && (Logical AND): Returns true if both operands are true.

  • || (Logical OR): Returns true if at least one of the operands is true.

void operatorExample() {
  int x = 23;
  int y = 27;

  // Arithmetic operators
  final add = x + y;                  // Addition
  final sub = x - y;                  // Subtraction
  final mut = x * y;                  // Multiplication
  final div = x / y;                  // Division
  final divwithintegers = y ~/ x;     // Truncating Division (returns an integer)
  final modulo = x % y;               // Modulus (remainder of division)

  // Relational operators
  final greater = x > y;              // Greater than
  final notGreater = x < y;           // Less than
  final greaterthan = x >= y;         // Greater than or equal to
  final notgreaterthan = x <= y;      // Less than or equal to

  // Equality operators
  final equalTo = x == y;             // Equal to
  final notEqualTo = x != y;          // Not equal to

  // Logical operators
  final logicalAnd = x > y && y < x;  // Logical AND
  final logicalOr = x > y || y < x;   // Logical OR

  // Printing results
  print("Addition of two numbers: $add");
  print("Subtraction of two numbers: $sub");
  print("Multiplication of two numbers: $mut");
  print("Division of two numbers: $div");
  print("Divide, returning an integer result: $divwithintegers");
  print("Remainder of an integer division: $modulo");
  print("Greater than: $greater");
  print("Less than: $notGreater");
  print("Greater than or equal to: $greaterthan");
  print("Less than or equal to: $notgreaterthan");
  print("Equal to: $equalTo");
  print("Not equal to: $notEqualTo");
  print("Logical AND: $logicalAnd");
  print("Logical OR: $logicalOr");
}

These operators are fundamental for performing arithmetic calculations, making comparisons, and evaluating conditions in Dart programs.

Conclusion:

Dart’s versatility and simplicity make it an excellent choice for developers across various domains. Understanding these fundamental concepts equips you to write efficient Dart code for diverse applications, ensuring clarity, reliability, and performance. Happy coding with Dart!

Start Coding in Dart Now!

Head over toDartPad(dartpad.dev) to start coding immediately. DartPad is a user-friendly online editor where you can write, run, and share Dart code without any setup required.

Buy Me A Coffee

Did you find this article valuable?

Support Sadanand gadwal 's blog by becoming a sponsor. Any amount is appreciated!