Dart – Print without Newline

To print a string to console without a trailing new line in Dart, import dart:io library, and call stdout.write() function with the string passed as argument to it.

Examples

In the following example, we print numbers from 1 to 20, to console, without a trailing newline.

main.dart

import 'dart:io';

void main() {
  var i = 1;
  while (i <= 20) {
    stdout.write(i);
    i++;
  }
}

Output

1234567891011121314151617181920

We may append a space or newline manually, if required.

In the following example, we print a space after each number.

main.dart

import 'dart:io';

void main() {
  var i = 1;
  while (i <= 20) {
    stdout.write(i);
    stdout.write(' ');
    i++;
  }
}

Output

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
ADVERTISEMENT

Conclusion

In this Dart Tutorial, we learned how to print to console without a trailing newline, with examples.