What is the output?
Dart infers the type as int because 10 is a whole number.
Learn Dart variables the practical way
Passing the Quiz & Exercises is mandatory to earn FX and proceed. If you fail, try again to gain the required FX, then the Proceed button will become available.
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Variables are used to store data in your program. Dart provides several ways to declare variables, and the language can often figure out the correct type for you automatically.
1// --- Type inference with 'var' ---2// Dart automatically detects the type34var name = 'John'; // Detected as String5var age = 25; // Detected as int6var price = 19.99; // Detected as double7var isLoggedIn = true; // Detected as bool8910// --- Explicit type declaration ---11// You specify the type yourself1213String city = 'New York'; // Text14int count = 100; // Whole number15double rating = 4.5; // Decimal number16bool isDarkMode = false; // True/False
Use `var` for local variables where the type is obvious. Use explicit types for class fields and public APIs.
Both final and const make variables unchangeable once they are set, but they work differently.
1// final - set once, can be runtime value2final username = getUserName(); // OK3final now = DateTime.now(); // OK45// const - must be compile-time constant6const pi = 3.14159; // OK7const maxItems = 100; // OK8// const now = DateTime.now(); // ERROR!
Dart includes built-in null safety, which means variables cannot hold a null value unless you explicitly allow it.
1String name = 'John'; // Cannot be null2String? nickname; // Can be null (note the ?)34// Null-aware operators5print(nickname ?? 'No nickname'); // Use default if null6print(nickname?.length); // Safe access, returns null if nickname is null
Forgetting `?` for nullable types is the #1 beginner error. If a value might be null, use `String?` not `String`.
Use this quick rule set while writing Dart:
- Use var for short local variables where type is obvious
- Use explicit types for class fields and function signatures
- Use final by default for values that should not change
- Use const only for compile-time constants
These habits reduce bugs and make code easier for teammates to understand quickly.
Learn Dart variables the practical way. Dart is the language behind Flutter, and clear Dart code makes your Flutter UI easier to maintain.
Build confidence with basics first; speed comes later.
Prefer readable names, explicit intent, and async/await over deeply nested callbacks.
When Dart basics are weak, state bugs, null crashes, and async confusion increase quickly.
Dart infers the type as int because 10 is a whole number.
final variables can only be set once, but the value can be determined at runtime.
The ? after the type indicates the variable can hold null.
The ?? operator returns the right side if the left side is null.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue