int: Represents whole numbers.
double: Represents fractional numbers.
bool: Represents a Boolean value (true or false).
String: Represents a sequence of characters.
List: An ordered collection of items, like an array.
Map: A collection of key-value pairs.
Set: An unordered collection of unique items.
Runes: For representing Unicode characters (such as emojis).
Symbol: Represents a compile-time constant symbol.
dynamic: A variable that can change its type at runtime.
Nullable Types: Using ? after a type allows it to be null.
const and final: const is for compile-time constants, while final is for variables that are initialized only once.
// Integer
int age = 25;
print("Age: $age");
// Double
double height = 5.9;
print("Height: $height");
// Boolean
bool isStudent = true;
print("Is Student: $isStudent");
// String
String name = "Alice";
print("Name: $name");
// List (can be homogeneous or heterogeneous if type is not specified)
List<int> numbers = [1, 2, 3, 4, 5];
print("Numbers: $numbers");
// Map (key-value pair)
Map<String, String> person = {
'firstName': 'Alice',
'lastName': 'Johnson',
};
print("Person: $person");
// Set (unique values)
Set<String> colors = {'R', 'G', 'B'};
print("Colors: $colors");
// Symbol
Symbol symbol = #dartLang;
print("Symbol: $symbol");
// Runes (for Unicode characters)
Runes emoji = Runes('\u{1F600}'); // 😀 emoji
print("Emoji: ${String.fromCharCodes(emoji)}");
// Dynamic (can change its type)
dynamic variable = "Hello";
print("Dynamic variable initially: $variable");
variable = 42;
print("Dynamic variable changed to: $variable");
// Null safety
String? nullableString;
print("Nullable String: $nullableString");
nullableString = "Now it has a value!";
print("Updated Nullable String: $nullableString");
// Const and final
const double pi = 3.14159; // Constant value at compile time
final DateTime currentTime = DateTime.now(); // Final value at runtime
print("PI: $pi");
print("Current Time: $currentTime");
}
Data types
Variables
Operators
user input
if
if_Else
if_Else_if ladder
Switch case
for, for_in, for_each
while
do_While
Break and Continue
1. Functions (Definition, Declaration, Calling)
2. Function Arguments (Positional, Named)
3. Return Types
4. Closures
5. Higher-Order Functions
Data types
Variables
Operators
user input
Explanation:-
Data types specify what kind of data a variable can hold. Dart is statically typed, so each variable has a specific data type, either explicitly declared or inferred.
Common Data Types in Dart:
int: Holds integer values.
double: Holds decimal numbers (floating-point).
String: Holds a sequence of characters.
bool: Holds either true or false.
List: An ordered collection of items.
Map: A collection of key-value pairs.
Variables are containers that store data values. In Dart, you declare variables using var, final, or const.
var: Type is inferred based on the assigned value and can be reassigned.
final: Value can only be set once and cannot be changed.
const: Similar to final but is a compile-time constant.
Dart provides several types of operators, such as arithmetic, relational, logical, and assignment operators.
Examples of Operators:
Arithmetic Operators: +, -, *, /, %
Relational Operators: ==, !=, >, <, >=, <=
Logical Operators: && (and), || (or), ! (not)
Assignment Operators: =, +=, -=, etc.
Dart has no native way to take user input directly. However, if you’re using Dart in a console environment (like Dart CLI), you can use the dart:io library to take input.
In this example:
stdin.readLineSync() is used to take input as a string.
int.parse() converts a string input to an integer.
Data Types:
Example:
void main() {
int age = 25; //integer types
double price = 99.99; //Double types
String name = "John"; //String types
bool isLoggedIn = true; //Boolean types types
List<int> numbers = [1, 2, 3, 4]; //List types
Map<String, String> user = {'name': 'Alice', 'country': 'USA'}; //Map types types
print("Age: $age");
print("Price: $price");
print("Name: $name");
print("Is Logged In: $isLoggedIn");
print("Numbers: $numbers");
print("User Info: $user");
}
Variables-
Example:
void main() {
var name = "Emma"; // inferred as String
final int age = 30; // explicitly declared as int
const double pi = 3.14;
print("Name: $name");
print("Age: $age");
print("PI: $pi");
}
Operators:-
Example:
void main() {
int a = 10;
int b = 5;
// Arithmetic operations
print("Addition: ${a + b}");
print("Subtraction: ${a - b}");
print("Multiplication: ${a * b}");
print("Division: ${a / b}");
print("Modulo: ${a % b}");
// Relational operations
print("Is a greater than b? ${a > b}");
print("Is a equal to b? ${a == b}");
// Logical operations
bool isSunny = true;
bool isWeekend = false;
print("Is it sunny and weekend? ${isSunny && isWeekend}");
print("Is it sunny or weekend? ${isSunny || isWeekend}");
}
Input and Output:
Example:
import 'dart:io';
void main() {
print("Enter your name:");
String? name = stdin.readLineSync();
print("Enter your age:");
int age = int.parse(stdin.readLineSync()!);
print("Hello, $name! You are $age years old.");
}
Integer Input: Using int.parse(stdin.readLineSync()!) to read and parse an integer.
Double Input: Using double.parse(stdin.readLineSync()!) to read and parse a double.
String Input: Simply using stdin.readLineSync()! to read a string.
Boolean Input: Taking input as a string (e.g., yes/no) and converting it to a Boolean value.
List Input: Reading a comma-separated string, splitting it, and converting each item to an integer with map(int.parse).
//we can take user input using the dart:io library.
import 'dart:io';
void main() {
// Taking an integer input
print("Enter your age:");
int age = int.parse(stdin.readLineSync()!);
print("You entered age: $age");
// Taking a double input
print("Enter your height in meters:");
double height = double.parse(stdin.readLineSync()!);
print("You entered height: $height meters");
// Taking a string input
print("Enter your name:");
String name = stdin.readLineSync()!;
print("Hello, $name!");
// Taking a boolean input
print("Are you a student? (yes/no):");
String isStudentInput = stdin.readLineSync()!.toLowerCase();
bool isStudent = isStudentInput == 'yes';
print("Student status: $isStudent");
// Taking a list of numbers (comma-separated)
print("Enter a list of numbers separated by commas:");
String numbersInput = stdin.readLineSync()!;
List<int> numbers = numbersInput.split(',').map(int.parse).toList();
print("You entered the numbers: $numbers");
// Calculating and outputting some derived values
int nextAge = age + 1;
double heightInCm = height * 100;
print("Next year, you will be $nextAge years old.");
print("Your height in centimeters is $heightInCm cm.");
}
OutPuts:
Enter your age:
25
You entered age: 25
Enter your height in meters:
1.75
You entered height: 1.75 meters
Enter your name:
Alice
Hello, Alice!
Are you a student? (yes/no):
yes
Student status: true
Enter a list of numbers separated by commas:
1,2,3,4,5
You entered the numbers: [1, 2, 3, 4, 5]
Next year, you will be 26 years old.
Your height in centimeters is 175.0 cm.