Dart Variables and Data Types

Dart is the language behind Flutter. Before building screens, you learn how Dart stores and handles data. This topic covers variables, data types, and type safety — the building blocks of every Dart program.

What Is a Variable

A variable is a named container that holds a value. Think of it like a labeled box.

  ┌──────────┐      ┌───────────┐
  │  age     │  =   │    25     │
  └──────────┘      └───────────┘
   (label)             (value)

Declaring Variables in Dart

Dart offers several ways to declare a variable:

// Explicit type
int age = 25;
String name = 'Ravi';
double price = 49.99;
bool isLoggedIn = true;

// Type inferred with var
var city = 'Mumbai';  // Dart knows this is a String

// Value never changes — use final or const
final String country = 'India';
const double pi = 3.14159;

Core Data Types in Dart

TypeWhat It StoresExample
intWhole numbersint score = 100;
doubleDecimal numbersdouble gpa = 3.8;
StringTextString name = 'Sara';
boolTrue or falsebool isOnline = false;
dynamicAny type (avoid when possible)dynamic x = 42;

var vs final vs const

  var score = 10;
  score = 20;       // ✓ Allowed — value can change

  final score = 10;
  score = 20;       // ✗ Error — final set at runtime, cannot reassign

  const pi = 3.14;
  pi = 3;           // ✗ Error — const set at compile time, never changes

When to Use Each

  • Use var for values that change during the app's life.
  • Use final for values assigned once (like a user's name after login).
  • Use const for values known before the app runs (like a fixed API base URL).

String Tricks in Dart

String Interpolation

Insert variables directly into a string using $.

String name = 'Priya';
int age = 22;

print('My name is $name and I am $age years old.');
// Output: My name is Priya and I am 22 years old.

Multi-line Strings

String message = '''
Hello,
Welcome to Flutter!
''';

Null Safety in Dart

Dart uses null safety by default. This means a variable cannot be null unless you explicitly allow it. This prevents many common crashes.

  Without null safety (older languages):
  String name = null;   // Compiles. Crashes when used.

  Dart with null safety:
  String name = null;   // ✗ Error at compile time
  String? name = null;  // ✓ The ? means null is allowed

Null Safety Diagram

  Non-nullable variable:       Nullable variable:
  ┌────────────────────┐       ┌────────────────────┐
  │ String name        │       │ String? nickname   │
  │ Must hold a value  │       │ Can hold null      │
  │ Cannot be null     │       │ Check before use   │
  └────────────────────┘       └────────────────────┘

The Null-Aware Operators

String? nickname;

// Use ?? to provide a fallback
print(nickname ?? 'Guest');   // Prints: Guest

// Use ?. to call methods safely
print(nickname?.toUpperCase());  // Prints nothing (null)

Type Conversion

Dart does not convert types automatically. You convert them manually.

int x = 10;
double y = x.toDouble();   // int to double
String s = x.toString();  // int to String

String numStr = '42';
int parsed = int.parse(numStr);   // String to int

Common Variable Mistakes

MistakeFix
Assigning wrong type: int x = 'hello';Use correct type: String x = 'hello';
Using null without ?Declare as String? x;
Reassigning a final variableUse var if value changes
Forgetting int.parse() for string-to-numberAlways parse explicitly

Leave a Comment

Your email address will not be published. Required fields are marked *