← Dart tutorial

Dart

Variables & Data Types

Declare a variable with var and Dart infers its type from the assigned value.

Example: the three declaration keywords

void main() {
  var name = 'Ada';       // type inferred, can be reassigned
  final age = 21;          // set once, never reassigned
  const pi = 3.14159;      // a true compile-time constant
  print('$name is $age years old.');
}
Ada is 21 years old.

Use final for a value that's set once and never reassigned after that (common for something computed at runtime, like a result from an API call). Use const specifically for a value known and fixed at compile time, a genuine constant, not just "I don't plan to change this." In practice, prefer final by default for anything that won't be reassigned, and reach for var only when a variable genuinely needs to change later.

Example
void main() {
  String name = 'Ada';
  int age = 21;
  print('$name is $age years old.');
}
Output
Ada is 21 years old.
Ready to go further?

These free tutorials are a preview of what a Code Secure Academy cohort covers, project work and all.

Explore our tracks

Cookie preferences

Choose what you're comfortable with. You can change this any time from the footer.

Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account