paint-brush
Exploring Dart Fundamentals - Part 1by@sadanandgadwal
161 reads

Exploring Dart Fundamentals - Part 1

by Sadanand GadwalMarch 12th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Dart is a modern programming language that caters to various development needs. In this comprehensive guide, we’ll explore the foundational concepts of Dart through practical examples. Dart is a statically typed language, meaning each variable has a specific data type known at compile-time.
featured image - Exploring Dart Fundamentals - Part 1
Sadanand Gadwal HackerNoon profile picture


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 varfinal, 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
  • finalfinal 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.
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 toward 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 to DartPad (https://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.


Also published here