Learn/Dart for Flutter/Variables & Data Types

Variables & Data Types

Learn Dart variables the practical way

4 min~5 min read+25 FX

You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.

Table of Contents

  1. 1. final vs const
  2. 2. Null Safety
  3. 3. Practical Type Choice Rules
  4. 4. This Lesson In Simple Words
  5. 5. Your level focus
  6. 6. One practical habit
  7. 7. Deep Understanding Guide
  8. 8. Why this lesson matters
  9. 9. Study flow for better retention
  10. 10. Quick Glossary

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 in Dart

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.

dart
1// --- Type inference with 'var' ---
2// Dart automatically detects the type
3
4var name = 'John'; // Detected as String
5var age = 25; // Detected as int
6var price = 19.99; // Detected as double
7var isLoggedIn = true; // Detected as bool
8
9
10// --- Explicit type declaration ---
11// You specify the type yourself
12
13String city = 'New York'; // Text
14int count = 100; // Whole number
15double rating = 4.5; // Decimal number
16bool isDarkMode = false; // True/False

var vs explicit type

💡 var vs explicit type

Use `var` for local variables where the type is obvious. Use explicit types for class fields and public APIs.

final vs const

Both final and const make variables unchangeable once they are set, but they work differently.

dart
1// final - set once, can be runtime value
2final username = getUserName(); // OK
3final now = DateTime.now(); // OK
4
5// const - must be compile-time constant
6const pi = 3.14159; // OK
7const maxItems = 100; // OK
8// const now = DateTime.now(); // ERROR!

Null Safety

Dart includes built-in null safety, which means variables cannot hold a null value unless you explicitly allow it.

dart
1String name = 'John'; // Cannot be null
2String? nickname; // Can be null (note the ?)
3
4// Null-aware operators
5print(nickname ?? 'No nickname'); // Use default if null
6print(nickname?.length); // Safe access, returns null if nickname is null

Common Mistake

⚠️ Common Mistake

Forgetting `?` for nullable types is the #1 beginner error. If a value might be null, use `String?` not `String`.

Practical Type Choice Rules

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
  • Make values nullable only when null is a real, valid state

These habits reduce bugs and make code easier for teammates to understand quickly.

Before You Move On

  • I can explain var, final, and const with one practical example each.
  • I can use nullable types and null-aware operators without guessing.
  • I can identify when explicit type annotations improve readability.
  • I can refactor one weakly typed variable into safer typed code.

This Lesson In Simple Words

Learn Dart variables the practical way. Dart is the language behind Flutter, and clear Dart code makes your Flutter UI easier to maintain.

Your level focus

Build confidence with basics first; speed comes later.

One practical habit

Prefer readable names, explicit intent, and async/await over deeply nested callbacks.

Deep Understanding Guide

Why this lesson matters

When Dart basics are weak, state bugs, null crashes, and async confusion increase quickly.

Study flow for better retention

  1. Read one section, run one tiny example, then explain the idea in your own words.
  2. Treat each Dart concept as a building block for widgets, services, and tests you will write later.
  3. If you can teach it in 45 seconds without notes, you understood it.

Common Mistakes To Avoid

  • Ignoring null safety and forcing values with ! without checking data flow.
  • Writing long functions that mix validation, transformation, and side effects together.
  • Using dynamic where a proper type would catch mistakes earlier.
  • Catching broad errors without keeping useful context for debugging.

Practice Sprint (15-25 Minutes)

  • Before running sample code, predict output and behavior in one sentence.
  • Write a small pure function and test it with edge cases.
  • Convert one callback-based snippet to async/await with proper error handling.
  • Refactor a long function into smaller helper methods with clear names.
  • Complete the required exercises without using hints on the first attempt.
  • Answer this question out loud before checking quiz options: "What is the output?".

Quick Glossary

  • state: Data that can change and update what the user sees.
  • const: A compile-time constant that can reduce rebuild work.
  • null safety: Dart type system feature that prevents null reference errors.

Code Comprehension Drill

  • State the purpose of the code block in one line before reading details.
  • Mark inputs, state changes, and final output.
  • Change one small value and predict what should happen before running.
  • Run again and compare your prediction with actual behavior.

Lesson Completion Checklist

  • I can explain "Variables & Data Types" in simple words without reading notes.
  • I can describe one real app situation where this concept is required.
  • I know at least one mistake to avoid: Ignoring null safety and forcing values with ! without checking data flow.
  • I can modify one code sample and correctly predict the behavior change.
  • I can finish required exercises with understanding, not trial-and-error guesses.

Frequently Asked Questions

What is the output?

Dart infers the type as int because 10 is a whole number.

Which keyword makes a variable that can only be set once?

final variables can only be set once, but the value can be determined at runtime.

How do you declare a nullable String?

The ? after the type indicates the variable can hold null.

What is the output?

The ?? operator returns the right side if the left side is null.

Suggest Improvement

Something seems incorrect, unclear, or missing? Let us know.

About The Author

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