HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to DartVariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Map
🔀 Condition & Loop
🧩 Function
Dart›Data Types

Data Types

Basic types like int, double, String, bool; type inference and text↔number conversion.

Simple enough for a 5-year-old to understand

The four basic types

Example code
1
void main() {
2
int number = 7;
Integer.
VariableInteger.
3
double decimal = 3.14;
Decimal.
VariableDecimal.
4
String text = "Dart";
Text.
VariableText.
5
bool isTrue = true;
Boolean: true/false.
VariableBoolean: true/false.
6
print("$number $decimal $text $isTrue");
7 3.14 Dart true author.
Output / Input7 3.14 Dart true author.
7
}
VariableOutput / Input
💡 Hint: $degisken is used to embed values into text (string interpolation).

About this topic

Dart's basic types are int, double, String, bool, List, Map, and Set. The common supertype of int and double is num; functions that accept both use the num type.

In Dart, everything is an object, even numbers and booleans. Therefore, you can call methods on a number; integers have their own methods. This design makes the language easy to learn because there are few exception rules.

Null safety is at the center of the type system. int and int? are two distinct types: the first can never be null, the second can. You must check a nullable value before using it; the compiler enforces this.

What you will learn

  • Basic types and the num hierarchy
  • The difference between integer and decimal division
  • Nullable types
  • Type conversions
  • The risk of the dynamic type

Who is this for?

  • Newcomers to the type system
  • Those who get unexpected values as a result of division
  • Those encountering null reference errors

When to use?

Fields coming from the server that might be missing should be modeled with nullable types; this makes it impossible to skip the check.

Double produces rounding errors in monetary calculations; when cent precision is needed, storing cents as integers is a common solution.

Common mistakes

Wrong

void main() {
  int a = 7;
  int b = 2;
  int sonuc = a / b;
  print(sonuc);
}

In Dart, a single slash always produces a double, even if two integers are divided. Assigning the result to an int variable throws the error "A value of type 'double' can't be assigned to a variable of type 'int'". There is a separate operator for integer division.

Correct

void main() {
  int a = 7;
  int b = 2;
  int sonuc = a ~/ b;
  print(sonuc);
}

Wrong

void main() {
  String? name;
  print(name.length);
}

You cannot directly access a nullable value; you will get the error "The property 'length' can't be unconditionally accessed because the receiver can be 'null'". A safe access operator or a null check is required.

Correct

void main() {
  String? name;
  print(name?.length ?? 0);
}

Wrong

void main() {
  dynamic value = "hello";
  print(value + 5);
}

The dynamic type turns off all type checks; the compiler accepts this code, but you will get a runtime type error. dynamic disables all protection provided by the type system with a single word and should only be used when truly necessary.

Correct

void main() {
  String value = "hello";
  print(value + "5");
}

Frequently asked questions

How to convert between int and double?

With toInt and toDouble methods. toInt discards the decimal part, it does not round; if you want to round, round is used, and to round down, floor is used. Dart does not perform these conversions automatically, you must write them explicitly.

Are var and dynamic the same thing?

No. var performs type inference and the type remains fixed once determined; dynamic, on the other hand, leaves the type completely free and turns off checks. var is safe, dynamic is not.

How do I convert text to a number?

int.parse or double.parse is used; if the conversion fails, it throws an error. If you don't want an error, use int.tryParse, it returns null on failure and you can provide a fallback value. tryParse is safer for user input.

Summary

The common supertype of int and double is num.

A single slash always produces a double.

There is a separate operator for integer division.

A nullable type is accessed with the safe access operator.

dynamic turns off type checks.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code