Understanding Data Types in Dart: Complete Guide for Flutter Developers

Understanding Data Types in Dart: Complete Guide for Flutter Developers

Dart is a modern, object-oriented, and type-safe programming language developed by Google. It is the primary language used for Flutter app development. Understanding data types is essential because they determine what kind of values a variable can store.


Data type in dart


What is Type Safety?

Dart is a type-safe language, which means a variable can only store values of its declared type. This helps catch errors during compilation instead of runtime.

String name = "Virat";
name = "Flutter"; // Valid

name = 100; // Error
Benefits of Type Safety:
  • Fewer runtime errors
  • Better code readability
  • Strong IDE support
  • Easier maintenance

Built-in Data Types in Dart

Built-in data types are provided by Dart by default and are used in everyday programming.

Data Type Description
int Stores whole numbers
double Stores decimal numbers
num Parent class of int and double
String Stores text
bool Stores true or false
List Ordered collection of items
Set Unordered Collection of unique items
Map Stores key-value pairs

1. Number

Number represents numeric values in Dart. It includes:

  • int - Whole numbers
  • double - Decimal numbers
int age = 25;
double pi = 3.14;

2. String

String represents text values.

String name = "Flutter";
String city = 'Mumbai';

3. Boolean (bool)

Stores only true or false values.

bool isLoggedIn = true;
bool isAdmin = false;

4. List

An ordered collection of items.

List fruits = [
  "Apple",
  "Banana",
  "Orange"
];

5. Set

An unordered collection of unique items.

Set numbers = {1, 2, 3, 4};

Duplicate values are automatically removed.

Set values = {1,1,2,2};
print(values);

// Output: {1,2}

6. Map

Stores data in key-value pairs.

Map user = {
  "name":"Rohan",
  "city":"Mumbai"
};

Special Data Types in Dart

7. Dynamic

Can hold any type of value.

dynamic data = "Hello";
data = 100;
data = true;

8. Object

The parent class of all Dart objects.

Object value = "Flutter";

9. Null

Represents the absence of a value.

String? username = null;

10. Runes

Used to work with Unicode characters and emojis.

String heart = "❤️";
print(heart.runes);

11. Symbol

Represents an identifier.

Symbol s = #myVariable;

12. Type

Represents a Dart type at runtime.

print(int);
print(String);

Type Conversion in Dart

String to int

String age = "25";
int result = int.parse(age);

String to double

String price = "99.99";
double result = double.parse(price);

int to String

int age = 25;
String value = age.toString();

double to String

double pi = 3.14;
String value = pi.toString();

int to double

int number = 10;
double result = number.toDouble();

double to int

double price = 99.99;
int result = price.toInt();

Safe Conversion using tryParse()

String value = "abc";

int result = int.tryParse(value) ?? 0;
Interview Tip: Always prefer tryParse() over parse() when dealing with user input.

Conclusion

Data types are the foundation of every Dart and Flutter application. Understanding built-in data types such as Number, String, Boolean, List, Set, and Map, along with special data types like Dynamic, Object, Null, Runes, Symbol, and Type, helps developers write safer and more maintainable code.

Mastering type conversion methods such as parse(), tryParse(), toString(), toInt(), and toDouble() is equally important because real-world applications frequently work with APIs, databases, and user input.