.toString()

Published May 10, 2024
Contribute to Docs

In Dart, the .toString() method converts the object into a string representation.

Syntax

typeObject.toString()

Above, typeObject.toString() calls the toString() method on the object typeObject.

Example

The following example demonstrates how the .toString() method is used to convert numbers to a string:

void main() {
int numbers = 123;
var result = numbers.toString();
print(result);
}

The code shown above will generate the following output:

123

Note: The output 123 is a string. This can be confirmed by checking result is String and result.runtimeType.

All contributors

Looking to contribute?

Learn Dart on Codecademy