Dart Basics: Variables, Types and Operators
Why Learn Dart First?
Flutter apps are written in Dart. Dart looks familiar if you know Java, JavaScript, C# or Kotlin. In this module you learn only what you need for Flutter. You can try every example online in DartPad at dartpad.dev with no installation.
Your First Dart Program
dart
void main() {
print('Hello, Dart!');
}
Every Dart program starts with the main() function. Statements end with a semicolon.
Variables
dart
void main() {
String name = 'Asha';
int age = 25;
double height = 1.62;
bool isStudent = true;
print('$name is $age years old');
}
| Type | Stores | Example |
|---|---|---|
int | Whole numbers | 42 |
double | Decimal numbers | 3.14 |
String | Text | 'hello' or "hello" |
bool | true or false | true |
List | Ordered collection | [1, 2, 3] |
Map | Key and value pairs | {'a': 1} |
dynamic | Any type (avoid when possible) | anything |
var, final and const
dart
var city = 'Mumbai'; // type is inferred as String
city = 'Delhi'; // allowed
final today = DateTime.now(); // set once at runtime, cannot change after
const pi = 3.14159; // compile-time constant, never changes
| Keyword | Can change later? | Value known when? |
|---|---|---|
var | Yes | Any time |
final | No (set only once) | At runtime |
const | No | Before the program runs |
Good habit: Prefer
final for variables that do not change. It prevents mistakes. Use const for fixed values and fixed widgets.
String Interpolation
dart
var item = 'Coffee';
var price = 3.5;
print('$item costs \$${price * 2} for two'); // Coffee costs $7.0 for two
print('Name length: ${item.length}'); // use {} for expressions
Useful String Methods
dart
var s = ' Flutter is fun ';
print(s.trim()); // Flutter is fun
print(s.toUpperCase());
print(s.contains('fun')); // true
print(s.replaceAll('fun', 'great'));
print('a,b,c'.split(',')); // [a, b, c]
Numbers
dart
int a = 10;
double b = 3.0;
print(a / 4); // 2.5 (division always gives a double)
print(a ~/ 4); // 2 (integer division)
print(a % 3); // 1 (remainder)
print(a.toDouble());
print(3.7.round()); // 4
print(int.parse('42') + 1); // 43
Operators
| Kind | Operators |
|---|---|
| Arithmetic | + - * / ~/ % |
| Comparison | == != > < >= <= |
| Logical | && || ! |
| Assignment | = += -= *= /= ??= |
| Increment | ++ -- |
| Null-aware | ?? ?. ??= ! |
Conclusion
You now know how to store data, join strings and do math in Dart. Use final by default and enjoy type inference.
Quick Summary: Dart has int, double, String and bool. Use var, final and const, and use $ for string interpolation.

