Type Conversion in Dart
In this lesson, you will learn about type conversion in Dart. Type conversion is the process of converting one data type into another, such as converting strings into numbers or numbers into strings.
In real-world applications, type conversion is extremely common when working with user input, APIs, databases, configuration files, and calculations.
Why Type Conversion Is Important
Most user inputs and API responses come as strings. To perform calculations or logical operations, these strings must be converted into numeric or boolean types.
Without proper type conversion, programs may crash or produce incorrect results.
Real-World Scenario
Imagine an e-commerce system where prices and quantities come as strings from a form or API response.
String priceText = "499.99";
String quantityText = "3";
To calculate the total cost, we must convert these strings into numbers.
String to Integer Conversion
Use int.parse() to convert a string into an integer.
int quantity = int.parse(quantityText);
print(quantity);
This is commonly used when dealing with counts, IDs, or quantities.
String to Double Conversion
Use double.parse() to convert a string into a decimal number.
double price = double.parse(priceText);
print(price);
This is important for prices, measurements, ratings, and percentages.
Performing Calculations After Conversion
Once converted, values can be used in arithmetic operations.
double totalCost = price * quantity;
print("Total Cost: $totalCost");
This approach is widely used in billing systems and dashboards.
Number to String Conversion
Use toString() to convert numbers into strings.
int orderId = 1024;
String orderText = orderId.toString();
print("Order ID: " + orderText);
This is useful when displaying values on the UI or logging data.
Double to Integer Conversion
Dart provides multiple ways to convert a double into an integer.
toInt()– removes decimal partround()– rounds to nearest integerceil()– rounds upfloor()– rounds down
double rating = 4.7;
print(rating.toInt());
print(rating.round());
print(rating.ceil());
print(rating.floor());
Safe Conversion Using tryParse()
If a string contains invalid data, parse() will throw an error.
To avoid crashes, use tryParse().
String input = "abc";
int? value = int.tryParse(input);
print(value);
If conversion fails, null is returned instead of crashing the app.
Boolean Conversion
Booleans are often derived from conditions or comparisons.
int age = 20;
bool isAdult = age >= 18;
print(isAdult);
This is widely used in validation and decision-making logic.
Converting Lists of Strings to Numbers
Real-world data often comes in lists.
List scoresText = ["85", "90", "78"];
List scores = scoresText.map(int.parse).toList();
print(scores);
This approach is common in analytics and reporting systems.
Common Type Conversion Mistakes
- Parsing invalid strings without validation
- Ignoring null values from
tryParse() - Using integers when decimals are required
- Forgetting localization (decimal separators)
📝 Practice Exercises
Exercise 1
Convert a string number into an integer and print it.
Exercise 2
Convert a price string into a double and calculate total cost.
Exercise 3
Safely parse an invalid number using tryParse().
✅ Practice Answers
Answer 1
String text = "100";
int value = int.parse(text);
print(value);
Answer 2
String price = "299.5";
double cost = double.parse(price);
print(cost * 2);
Answer 3
String data = "xyz";
int? result = int.tryParse(data);
print(result);
What’s Next?
In the next lesson, you will learn about Exception Handling in Dart,
including try, catch, finally,
and how to handle runtime errors safely.