Dart int
last modified June 4, 2025
In Dart, the int
type represents integer numbers. Integers are whole
numbers without fractional components. The int
type is one of Dart's
built-in primitive types.
Dart integers can be arbitrarily large, limited only by available memory. On the web, integers are restricted to 64 bits due to JavaScript number representation.
Basic Integer Declaration
The simplest way to create an integer is by using integer literals.
void main() { int age = 35; int temperature = -10; int population = 8000000000; print(age); print(temperature); print(population); }
We declare three integers with different values. Dart supports both positive and negative integers. Large numbers can use underscores for readability.
$ dart main.dart 35 -10 8000000000
Integer Operations
Dart supports standard arithmetic operations on integers.
void main() { int a = 10; int b = 3; print(a + b); // Addition print(a - b); // Subtraction print(a * b); // Multiplication print(a ~/ b); // Integer division print(a % b); // Modulo print(a & b); // Bitwise AND }
This shows basic arithmetic operations. Note the ~/ operator for integer division. Regular division (/) returns a double even with integer operands.
$ dart main.dart 13 7 30 3 1 2
Integer Properties and Methods
The int type provides useful properties and methods for number manipulation.
void main() { int num = 42; print(num.isEven); // true print(num.isOdd); // false print(num.isNegative); // false print(num.abs()); // 42 print(num.toDouble()); // 42.0 print(num.toString()); // "42" print(num.bitLength); // 6 }
We examine several properties and methods. isEven/isOdd check parity, abs gives absolute value, and bitLength returns minimum bits needed to represent the number.
$ dart main.dart true false false 42 42.0 42 6
Integer Parsing
Dart provides methods to parse integers from strings with error handling.
void main() { String numStr = "1234"; String invalidStr = "12a34"; int parsed1 = int.parse(numStr); print(parsed1); int? parsed2 = int.tryParse(invalidStr); print(parsed2); int hex = int.parse("FF", radix: 16); print(hex); }
parse throws FormatException for invalid input, while tryParse returns null. The radix parameter supports parsing different bases (2-36).
$ dart main.dart 1234 null 255
Integer Range and Limits
Dart integers have platform-dependent size limits we can check.
void main() { print("Max 64-bit signed int: ${1 << 63 - 1}"); print("Min 64-bit signed int: ${-1 << 63}"); BigInt hugeNum = BigInt.parse("123456789012345678901234567890"); print(hugeNum); int regularInt = 9007199254740991; // Max safe JS integer print(regularInt); }
For numbers beyond 64-bit range, use BigInt. On web platforms, integers are limited to 53 bits due to JavaScript's number representation.
$ dart main.dart Max 64-bit signed int: 9223372036854775807 Min 64-bit signed int: -9223372036854775808 123456789012345678901234567890 9007199254740991
Best Practices
- Type Inference: Use
var
when type is obvious from context. - Readability: Use underscores in large numbers (1_000_000).
- Error Handling: Prefer
tryParse
overparse
for user input. - Performance: Use
int
instead ofdouble
when appropriate.
Source
This tutorial covered Dart's int type with practical examples demonstrating its features and usage patterns.
Author
List all Dart tutorials.