Dart Fundamentals: The Language Behind Flutter · Dart Basics: Variables, Null Safety, Functions and Collections
Hello, Dart: Variables, Types and Strings
Write and run Dart programs, declare variables with var, final and const, use Dart's core types, and build strings with interpolation and multi-line literals.
What you'll build
A small "trip budget" program: it stores costs, computes totals and per-person shares, and prints a neat summary with string interpolation.
The idea
Dart is a typed, object-oriented language from Google, best known as the language of Flutter (iOS, Android, web and desktop apps from one codebase). It also runs servers and command-line tools, and compiles to native code and to JavaScript or WebAssembly.
Every program starts at main(). Run a file with dart run main.dart; create a project with dart create my_app.
Variables:
var name = 'Ana';: type inferred (String), can be reassigned.final total = 42;: assigned once (at runtime).const pi = 3.14159;: a compile-time constant.- Explicit types work too:
int count = 0; double price = 9.99; bool done = false; String city = 'Lisbon';
Strings use single or double quotes, '$name' and '${expression}' for interpolation, and triple quotes for multi-line text. num covers both int and double.
The code
const currency = '€';
void main() {
final travellers = ['Ana', 'Ben', 'Cleo'];
var nights = 4;
final nightlyRate = 96.50;
final flights = 3 * 129.99;
nights += 1; // var can change; final and const can't
final hotel = nights * nightlyRate;
final total = hotel + flights;
final perPerson = total / travellers.length;
print('Trip for ${travellers.length}: ${travellers.join(', ')}');
print('Hotel ($nights nights): $currency${hotel.toStringAsFixed(2)}');
print('Flights: $currency${flights.toStringAsFixed(2)}');
print('Total: $currency${total.toStringAsFixed(2)}, '
'per person: $currency${perPerson.toStringAsFixed(2)}');
final summary = '''
Checklist:
- passports
- ${nights} nights booked''';
print(summary);
print('types: ${nights.runtimeType}, ${total.runtimeType}, ${travellers.runtimeType}');
print('int division: ${7 ~/ 2}, remainder: ${7 % 2}, double division: ${7 / 2}');
}
Output
Trip for 3: Ana, Ben, Cleo Hotel (5 nights): €482.50 Flights: €389.97 Total: €872.47, per person: €290.82 Checklist: - passports - 5 nights booked types: int, double, List<String> int division: 3, remainder: 1, double division: 3.5
Run this on your computer — see “Get Started” for setup.
Common mistakes
- Using
varfor everything. Preferfinalfor values that don't change; it documents intent and prevents bugs. constvsfinal.constneeds a value known at compile time;finalcan be computed at runtime.- Integer division surprise.
/always returns adoublein Dart; use~/for integer division. - Forgetting braces in interpolation.
'$user.name'interpolatesuserthen adds ".name"; write'${user.name}'.
Key points
main()is the entry point;dart runexecutes a file or project.var(reassignable),final(set once),const(compile-time constant).- Core types:
int,double,num,String,bool,List,Map,Set. $nameand${expr}interpolation;'''for multi-line strings.
Further reading: Dart language tour: variables, Built-in types.
Practice questions
What's the difference between final and const in Dart?
Answer
Both can only be assigned once. final values can be computed at runtime; const values must be compile-time constants (and const collections are deeply immutable).
What does `7 / 2` return in Dart, and how do you get integer division?
Answer
3.5, a double: / always returns a double. Use ~/ for integer division (7 ~/ 2 == 3).
Open in the app to mark lessons complete, take notes and track your progress.