Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Type Conversion


Share

Dart Type Conversion

Type conversion means changing a value from one type to another type.

For example, you can change the string '25' into the number 25.

Dart does not change types for you.

If you mix a string and a number with +, the program does not run:

Example

void main() {
  print('Age: ' + 25);
}
Try it Yourself »

Result:

main.dart:2:19: Error: A value of type 'int' can't be assigned to a variable of type 'String'.
  print('Age: ' + 25);
                  ^

You must convert the value yourself, with the methods on this page.


String to Number

Use int.parse() to convert a string to an integer.

Use double.parse() to convert a string to a decimal number:

Example

void main() {
  int age = int.parse('25');
  double price = double.parse('9.99');

  print(age + 1);
  print(price * 2);
}
Try it Yourself »

Result:

26
19.98

If the string is not a valid number, parse() stops the program with a FormatException error:

Example

void main() {
  int number = int.parse('abc');
  print(number);
}
Try it Yourself »

Result:

Unhandled exception:
FormatException: Invalid radix-10 number (at character 1)
abc
^

Note: int.parse('3.5') also fails, because 3.5 is not a whole number.



Safe Conversion with tryParse()

int.tryParse() and double.tryParse() work like parse(), but they never stop the program.

If the string is not a valid number, they return null, which means "no value":

Example

void main() {
  print(int.tryParse('42'));
  print(int.tryParse('abc'));
  print(double.tryParse('2.5'));
  print(double.tryParse('hello'));
}
Try it Yourself »

Result:

42
null
2.5
null

Use tryParse() when the text comes from a user, because users can type anything.

You will learn how to work with null in the next chapter, Null Safety.


String to Boolean

Use bool.parse() to convert the text 'true' or 'false' to a bool:

Example

void main() {
  bool isMember = bool.parse('true');
  bool isAdmin = bool.parse('false');
  print(isMember);
  print(isAdmin);
  print(bool.tryParse('yes'));
}
Try it Yourself »

Result:

true
false
null

The text must be exactly 'true' or 'false', in small letters.

Other text, like 'yes' or 'True', makes bool.parse() stop the program, and bool.tryParse() return null.


Number to String

Use the toString() method to convert a number to a string.

After that, you can join it with other strings:

Example

void main() {
  int score = 95;
  String text = 'Score: ' + score.toString();
  print(text);
}
Try it Yourself »

Result:

Score: 95

Use toStringAsFixed() to get a string with a fixed number of decimals:

Example

void main() {
  double price = 4.5678;
  print(price.toStringAsFixed(2));
  print(price.toStringAsFixed(0));
}
Try it Yourself »

Result:

4.57
5

Tip: String interpolation, like 'Score: $score', also converts the number to a string for you.


int and double

Use toDouble() to convert an int to a double.

Use toInt() to convert a double to an int.

toInt() removes the decimals, it does not round:

Example

void main() {
  int apples = 5;
  double weight = 7.9;

  print(apples.toDouble());
  print(weight.toInt());
}
Try it Yourself »

Result:

5.0
7

To round to the nearest whole number, use round().

Use ceil() to round up, and floor() to round down:

Example

void main() {
  double x = 7.5;
  print(x.round());
  print(x.ceil());
  print(x.floor());
}
Try it Yourself »

Result:

8
8
7

A num can hold an int or a double.

Use toInt() when you need an int from a num:

Example

void main() {
  num temperature = 21.8;
  int whole = temperature.toInt();
  print(whole);

  num count = num.parse('12');
  print(count.toInt() + 3);
}
Try it Yourself »

Result:

21
15


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.