Dart Data Types and Variable Declarations



🎯 Dart Data Types and Variable Declarations – Explained with Examples


💡 Introduction

Every programming language uses data types to know what kind of data is being stored and manipulated. In Dart, a strongly-typed language, understanding data types helps you write more powerful and safer code.

Think of data types as the labels on storage boxes: some boxes store numbers, some hold text, others may contain just true/false switches.


📋 Keywords

  • int – Integer numbers

  • double – Decimal numbers (floating-point)

  • num – Can be either int or double

  • String – Text data

  • bool – Boolean (true or false)

  • var – Type inferred automatically

  • dynamic – Type that can change at runtime

  • const / final – Immutable variables


🧠 Dart Variables and Data Types Explained


🧮 1. int – Whole numbers

Stores non-decimal numbers (positive or negative).

int age = 25;

🧊 2. double – Decimal numbers

Stores floating-point numbers (like 3.14).

double pi = 3.1416;

➕ 3. num – Either int or double

Can hold both integer and decimal values.

num x = 10;
x = 3.14; // Valid

💬 4. String – Text

Used to store words, names, sentences.

String name = 'Shubham';

✅ 5. bool – True or False

For logical conditions.

bool isStudent = true;

📦 6. var – Type inference

Dart guesses the type from the right-hand value.

var city = 'Varanasi'; // Inferred as String

Once assigned, you can’t change its type:

// city = 123; ❌ Error

🔄 7. dynamic – Flexible type

Type can change at runtime.

dynamic value = 'Hello';
value = 42; // OK

🔐 8. final and const – Immutable variables

  • final: Value set once at runtime

  • const: Value set at compile time

final dob = '2000-01-01';
const pi = 3.14;

⚠️ Difference between var, final, and const

Keyword Can Reassign Fixed Type Immutable
var
final
const ✅ (at compile time)

✅ Solved Examples


1. Declare your name, age, and student status

void main() {
String name = 'Shubham';
int age = 22;
bool isStudent = true;
print('Name: $name');
print('Age: $age');
print('Is Student: $isStudent');
}

2. Calculate area of a circle using const pi

void main() {
const double pi = 3.1416;
double radius = 5;
double area = pi * radius * radius;
print('Area = $area');
}

3. Use of dynamic to change types

void main() {
dynamic x = 'Hello';
print(x);
x = 123;
print(x);
}

4. Type inference with var

void main() {
var city = 'Delhi'; // inferred as String
// city = 123; ❌ Error: type mismatch
print(city);
}

5. Error: Assigning wrong type

void main() {
int age = 25;
// age = 'twenty-five'; ❌ Compile-time error
}

❓ Unsolved Practice Problems

Try these in DartPad or your local Dart editor.


1. Create a program that stores:

  • Your full name

  • Your percentage in graduation (e.g., 82.5)

  • Whether you have programming experience (true/false)

Print the data in one sentence.


2. Ask the user for a product name and its price (use String and double) and print a receipt-like line:

Product: Pen | Price: ₹10.50

3. Use dynamic to store and print:

  • A name

  • Then a number

  • Then a boolean


4. Create a final variable for your birth year and try to reassign it. What happens?


5. Write a program where you use:

  • int for count

  • String for item name

  • bool for availability
    Then print a short inventory line like:

5 x Notebook - Available

🧾 Summary

  • Dart is strongly typed, but can be flexible using var and dynamic.

  • Use final or const for values that never change.

  • Use the correct data types to make your code clean, efficient, and bug-free.



Contact us for software training, education or development










 

Post a Comment

Me