Cambo Freelance
ទំព័រដើមសេវាកម្មអត្ថបទការបង្រៀនក្រុមវគ្គបណ្តុះបណ្តាលទំនាក់ទំនង
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

ផ្ញើសាររបស់អ្នក

ត្រៀមចាប់ផ្តើមគម្រោងរបស់អ្នក? ទំនាក់ទំនងក្រុមរបស់យើង។

ទំនាក់ទំនង

សេវាកម្ម

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

តំណភ្ជាប់មានប្រយោជន៍

  • ទំព័រដើម
  • សេវាកម្ម
  • ការសិក្សា
  • អំពីយើង
  • ទំនាក់ទំនង
  • តម្លៃ

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsDartType Conversion in Dart
🎯Dart Tutorials

97 lessons

Beginner(35)0Complete Dart Tutorial Series — Course Index1Introduction to Dart2Installing Dart SDK3Setting Up VS Code for Dart4DartPad Online Editor5Hello World in Dart6Dart Syntax7Comments in Dart8Variables in Dart9Data Types in Dart10Strings in Dart11Numbers in Dart12Booleans in Dart13Type Conversion in Dart14Constants in Dart: final and const15User Input in Dart16Arithmetic Operators in Dart17Assignment Operators in Dart18Comparison Operators in Dart19Logical Operators in Dart20Null-Aware Operators in Dart21If Statement in Dart22If Else in Dart23Else If in Dart25Switch Statement in Dart26Ternary Operator in Dart27For Loop in Dart28While Loop in Dart29Do While Loop in Dart30Break and Continue in Dart32Nested Loops in Dart33Functions in Dart35Optional Parameters in Dart37Arrow Functions in Dart38Recursive Functions in Dart
Intermediate(34)39Lists in Dart40Sets in Dart41Maps in Dart42Collection Operations in Dart
Advanced(28)73Futures in Dart74Async and Await in Dart75Streams in Dart76
🎯

Dart Tutorials

Lesson 13 of 97

All lessons
Beginner (35)0Complete Dart Tutorial Series — Course Index1Introduction to Dart
43
Spread Operator and Collection Literals in Dart
44Collection If in Dart
45Collection For in Dart
46Null Safety in Dart
47Nullable Variables in Dart
48Null Assertion Operator in Dart
49Late Keyword in Dart
50Required Keyword in Dart
51Classes and Objects in Dart
52Constructors in Dart
53Getters and Setters in Dart
54Static Members in Dart
55Interfaces in Dart
56Polymorphism in Dart
57Inheritance in Dart
58Method Overriding in Dart
59Abstract Classes in Dart
60Abstract Classes vs Interfaces in Dart
61Mixins in Dart
62Extension Methods in Dart
63Exception Handling in Dart
64Try-Catch in Dart
65Finally Block in Dart
66Custom Exceptions in Dart
67Generics in Dart
68Typedef in Dart
69Enums in Dart
70Records in Dart
71Pattern Matching in Dart
72Cascade Notation in Dart
Stream Controllers in Dart
77Isolates in Dart
78File Operations in Dart
80JSON Parsing in Dart
82HTTP Requests in Dart
83REST API Client in Dart
84Dart Packages and pub.dev
85Testing in Dart
86Dart CLI Applications
87Dart Code Style and Best Practices
88Server-side Dart with Shelf
89Introduction to Flutter
90Flutter Widgets and Layouts
91Flutter State Management
92Flutter Navigation and Routing
93Project: Calculator in Dart
94Project: Temperature Converter in Dart
95Project: Age Calculator in Dart
96Project: Todo CLI App in Dart
97Project: Student Management System in Dart
98Project: Expense Tracker in Dart
100Project: Banking System in Dart
101Project: Inventory Management System in Dart
102Project: Contact Manager in Dart
103Project: Mini REST API Backend in Dart
2
Installing Dart SDK
3Setting Up VS Code for Dart
4DartPad Online Editor
5Hello World in Dart
6Dart Syntax
7Comments in Dart
8Variables in Dart
9Data Types in Dart
10Strings in Dart
11Numbers in Dart
12Booleans in Dart
13Type Conversion in Dart
14Constants in Dart: final and const
15User Input in Dart
16Arithmetic Operators in Dart
17Assignment Operators in Dart
18Comparison Operators in Dart
19Logical Operators in Dart
20Null-Aware Operators in Dart
21If Statement in Dart
22If Else in Dart
23Else If in Dart
25Switch Statement in Dart
26Ternary Operator in Dart
27For Loop in Dart
28While Loop in Dart
29Do While Loop in Dart
30Break and Continue in Dart
32Nested Loops in Dart
33Functions in Dart
35Optional Parameters in Dart
37Arrow Functions in Dart
38Recursive Functions in Dart
Intermediate (34)39Lists in Dart40Sets in Dart41Maps in Dart42Collection Operations in Dart43Spread Operator and Collection Literals in Dart44Collection If in Dart45Collection For in Dart46Null Safety in Dart47Nullable Variables in Dart48Null Assertion Operator in Dart49Late Keyword in Dart50Required Keyword in Dart51Classes and Objects in Dart52Constructors in Dart53Getters and Setters in Dart54Static Members in Dart55Interfaces in Dart56Polymorphism in Dart57Inheritance in Dart58Method Overriding in Dart59Abstract Classes in Dart60Abstract Classes vs Interfaces in Dart61Mixins in Dart62Extension Methods in Dart63Exception Handling in Dart64Try-Catch in Dart65Finally Block in Dart66Custom Exceptions in Dart67Generics in Dart68Typedef in Dart69Enums in Dart70Records in Dart71Pattern Matching in Dart72Cascade Notation in Dart
Advanced (28)73Futures in Dart74Async and Await in Dart75Streams in Dart76Stream Controllers in Dart77Isolates in Dart78File Operations in Dart80JSON Parsing in Dart82HTTP Requests in Dart83REST API Client in Dart84Dart Packages and pub.dev85Testing in Dart86Dart CLI Applications87Dart Code Style and Best Practices88Server-side Dart with Shelf89Introduction to Flutter90Flutter Widgets and Layouts91Flutter State Management92Flutter Navigation and Routing93Project: Calculator in Dart94Project: Temperature Converter in Dart95Project: Age Calculator in Dart96Project: Todo CLI App in Dart97Project: Student Management System in Dart98Project: Expense Tracker in Dart100Project: Banking System in Dart101Project: Inventory Management System in Dart102Project: Contact Manager in Dart103Project: Mini REST API Backend in Dart
🎯DartBeginnerLesson 13 of 97Dart TeamJune 19, 2026

Type Conversion in Dart

Learn explicit type conversion in Dart — converting between String, int, double, and bool safely using parse(), toString(), toInt(), and toDouble().


📚 Table of Contents

  1. What is Type Conversion?
  2. String to Number
  3. Number to String
  4. int to double
  5. double to int
  6. Boolean Conversions
  7. Full Example
  8. Exercise
  9. Quiz
  10. Summary

What is Type Conversion?

Type conversion (or type casting) is the process of changing a value from one data type to another. In Dart this is always explicit — you must intentionally convert a value; Dart will not silently convert types for you.

String to Number

// String → int
String s1 = '42';
int n1 = int.parse(s1);
print(n1 + 8);   // 50

// String → double
String s2 = '3.14';
double n2 = double.parse(s2);
print(n2 * 2);   // 6.28

// Safe parsing (no exception on failure)
int? safe = int.tryParse('abc');
print(safe);     // null

Number to String

int n = 100;
double d = 3.14159;

String s1 = n.toString();           // '100'
String s2 = d.toString();           // '3.14159'
String s3 = d.toStringAsFixed(2);   // '3.14'
String s4 = n.toRadixString(16);    // '64' (hex)

print(s1 + ' dollars');             // 100 dollars

int to double

int i = 5;
double d = i.toDouble();   // 5.0
double d2 = i / 1;         // also 5.0 (division always returns double)
print(d);  // 5.0

double to int

double d = 9.99;
int i1 = d.toInt();       // 9  (truncates decimal)
int i2 = d.round();       // 10 (rounds to nearest)
int i3 = d.ceil();        // 10 (rounds up)
int i4 = d.floor();       // 9  (rounds down)
print('$i1 $i2 $i3 $i4'); // 9 10 10 9

Boolean Conversions

Dart does not automatically convert other types to bool. Use explicit comparisons:

int n = 0;
// bool b = n;  ← ERROR in Dart
bool b = n != 0;    // explicit conversion: false
bool b2 = n == 0;   // true

String s = '';
bool isEmpty = s.isEmpty;  // true

Full Example

void main() {
  // Simulating user input as strings
  String ageInput = '25';
  String heightInput = '5.9';

  int age = int.parse(ageInput);
  double height = double.parse(heightInput);

  print('Age: $age ($${age.runtimeType})');
  print('Height: $height ($${height.runtimeType})');

  // Convert back to string for display
  String summary = 'Age $age, Height $${height.toStringAsFixed(1)} ft';
  print(summary);

  // int ↔ double
  double ageDouble = age.toDouble();
  int heightInt = height.toInt();
  print('Age as double: $ageDouble');
  print('Height as int: $heightInt');
}
Output:
Age: 25 (int)
Height: 5.9 (double)
Age 25, Height 5.9 ft
Age as double: 25.0
Height as int: 5

💪 Exercise

  1. Parse '100' and '45.5' from strings, add them, and print the result.
  2. Convert the double 7.8 to an int using round(), floor(), and ceil() — print all three.
  3. Convert the integer 255 to its hexadecimal string representation.

🧠 Quiz

1. What does double.toInt() do to 3.9?

  • A) Returns 4 (rounds)
  • B) Returns 3 (truncates) ✅
  • C) Returns 3.9
  • D) Throws an error

2. Which method safely converts a string to int without throwing an exception?

  • A) int.parse()
  • B) int.convert()
  • C) int.tryParse() ✅
  • D) int.cast()

Summary

Dart type conversion is always explicit. Convert strings to numbers with int.parse() / double.parse(). Convert numbers to strings with toString() / toStringAsFixed(). Convert between int and double with toDouble() / toInt(). Use tryParse() for safe parsing that returns null on failure.

dartprogrammingbeginnertype-conversion
PreviousLesson 12: Booleans in DartNextLesson 14: Constants in Dart: final and const
Back to All Tutorials