Dart
Variables & Data Types
1. What is a Variable?
A variable is a named place where we store a value.
| Type | Example | Use |
| -------- | ---------- | --------------- |
| `String` | `"Hiruni"` | Text |
| `int` | `25` | Whole numbers |
| `double` | `99.99` | Decimal numbers |
| `bool` | `true` | True/false |String
Text store කරන්න.
String name = "Hiruni";
String city = "Colombo";
String message = "Hello Flutter";You can print:
void main() {
String name = "Hiruni";
print(name);
}Output:
Hiruniint
Whole numbers.
int age = 25;
int quantity = 10;
int score = 100;Example:
void main() {
int age = 25;
print(age);
}Output:
25You can’t normally do:
int age = "25";❌ Wrong.
Because "25" is a String, not an int.
double
Decimal numbers.
double price = 1500.50;
double rating = 4.5;
double temperature = 28.7;Example
void main() {
double price = 1500.50;
print(price);
}Output:
1500.5bool
Boolean means true or false.
bool isLoggedIn = true;
bool isAdmin = false;
bool hasPermission = true;Example:
void main() {
bool isLoggedIn = true;
print(isLoggedIn);
}Output:
trueThis becomes extremely important in Flutter.
For example:
bool isLoading = true;Then Flutter can decide:
isLoading == true
↓
Show loading spinnervar
Instead of:
String name = "Hiruni";You can write
var name = "Hiruni";Dart automatically understands:
name → Stringvar vs JavaScript var
This is especially important for you because you know JavaScript.
JavaScript
var value = 10;
value = "Hello";JavaScript allows this.
Dart
var value = 10;
value = "Hello"; ❌ Not allowed.final
final String name = "Hiruni";final means:
Assign the variable only once.
Example:
void main() {
final name = "Hiruni";
print(name);
}This is fine.
Get Hiruni Liyanage’s stories in your inbox
Join Medium for free to get updates from this writer.
But:
final name = "Hiruni";
name = "Tharindu"; ❌ Error.Because name is final.
variable එකට value එකක් එක් වතාවක් assign කළාට පස්සේ නැවත වෙනස් කරන්න බැහැ.
const
const appName = "SalesVision AI";const also means the value cannot change.
But there’s an important difference between final and const.
final
Value is decided at runtime.
final currentTime = DateTime.now();This is valid.
Why?
Because Dart only knows the current time when the program runs.
const
Value must be known at compile time.
const appName = "SalesVision AI";This is valid because "SalesVision AI" is known before the application runs.
final vs const
Remember this:
final → assigned once
const → compile-time constantEx:
final currentTime = DateTime.now();
const appName = "My App";final
↓
runtime value
↓
can be calculated when app runsconst
↓
compile-time value
↓
must already be knowndynamic
dynamic value = 10;
value = "Hello";
value = true;
value = 50.5;This is allowed.
Because dynamic tells Dart:
Don’t enforce a specific static type here.
But don’t use dynamic everywhere.
It removes many of Dart’s type-safety benefits.
var
→ Dart figures out the type
→ type remains fixed
dynamic
→ type can changeDart convention is camelCase:
firstName
productPrice
totalAmount
isLoggedIn
userIdnot
first_name
product_priceOperators
An operator performs an operation on values.
For example:
int a = 10;
int b = 5;
print(a + b); ---------> 15Arithmetic Operators
| Operator | Meaning | Example | Result |
| -------- | ---------------- | --------- | ------ |
| `+` | Add | `10 + 5` | `15` |
| `-` | Subtract | `10 - 5` | `5` |
| `*` | Multiply | `10 * 5` | `50` |
| `/` | Divide | `10 / 5` | `2.0` |
| `~/` | Integer division | `10 ~/ 3` | `3` |
| `%` | Remainder | `10 % 3` | `1` |Addition with Strings
+ can also join Strings.
String firstName = "Sri";
String lastName = "Lanka";
String fullName = firstName + " " + lastName;
print(fullName); -------->Sri LankaComparison Operators
| Operator | Meaning |
| -------- | --------------------- |
| `==` | Equal |
| `!=` | Not equal |
| `>` | Greater than |
| `<` | Less than |
| `>=` | Greater than or equal |
| `<=` | Less than or equal |Logical Operators
&&
||
!



