Skip to content

Stdout

class Stdout extends _StdSink implements IOSink

An IOSink connected to either the standard out or error of the process.

Provides a blocking IOSink, so using it to write will block until the output is written.

In some situations this blocking behavior is undesirable as it does not provide the same non-blocking behavior that dart:io in general exposes. Use the property nonBlocking to get an IOSink which has the non-blocking behavior.

This class can also be used to check whether stdout or stderr is connected to a terminal and query some terminal properties.

The addError API is inherited from StreamSink and calling it will result in an unhandled asynchronous error unless there is an error handler on done.

The lineTerminator field is used by the write, writeln, writeAll and writeCharCode methods to translate "\n". By default, "\n" is output literally.

Implemented types

Properties

done no setter inherited

Future<dynamic> get done

Inherited from _StdSink.

Implementation
dart
Future get done => _sink.done;

encoding read / write inherited

Encoding get encoding

getter:

The Encoding used when writing strings.

Depending on the underlying consumer, this property might be mutable.

setter:

The Encoding used when writing strings.

Depending on the underlying consumer, this property might be mutable.

Inherited from _StdSink.

Implementation
dart
Encoding get encoding => _sink.encoding;

void set encoding(Encoding encoding) {
  _sink.encoding = encoding;
}

hashCode no setter inherited

int get hashCode

The hash code for this object.

A hash code is a single integer which represents the state of the object that affects operator == comparisons.

All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).

If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.

Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.

Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.

If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.

Inherited from Object.

Implementation
dart
external int get hashCode;

hasTerminal no setter

bool get hasTerminal

Whether there is a terminal attached to stdout.

Implementation
dart
bool get hasTerminal => _hasTerminal(_fd);

lineTerminator read / write inherited

String get lineTerminator

Inherited from _StdSink.

Implementation
dart
&#47;&#47;
&#47;&#47;&#47; Setting `lineTerminator` to [Platform.lineTerminator] will result in
&#47;&#47;&#47; "write" methods outputting the line endings for the platform:
&#47;&#47;&#47;
&#47;&#47;&#47; ```dart
&#47;&#47;&#47; stdout.lineTerminator = Platform.lineTerminator;
&#47;&#47;&#47; stderr.lineTerminator = Platform.lineTerminator;
&#47;&#47;&#47; ```
&#47;&#47;&#47;
&#47;&#47;&#47; The value of `lineTerminator` has no effect on byte-oriented methods
&#47;&#47;&#47; such as [add].
&#47;&#47;&#47;
&#47;&#47;&#47; The value of `lineTerminator` does not effect the output of the [print]
&#47;&#47;&#47; function.
&#47;&#47;&#47;
&#47;&#47;&#47; Throws [ArgumentError] if set to a value other than `"\n"` or `"\r\n"`.
String get lineTerminator => _windowsLineTerminator ? "\r\n" : "\n";

set lineTerminator(String lineTerminator) {
  if (lineTerminator == "\r\n") {
    assert(!_lastWrittenCharIsCR || _windowsLineTerminator);
    _windowsLineTerminator = true;
  } else if (lineTerminator == "\n") {
    _windowsLineTerminator = false;
    _lastWrittenCharIsCR = false;
  } else {
    throw ArgumentError.value(
      lineTerminator,
      "lineTerminator",
      r'invalid line terminator, must be one of "\n" or "\r\n"',
    );
  }
}

nonBlocking no setter

IOSink get nonBlocking

A non-blocking IOSink for the same output.

The returned IOSink will be initialized with an encoding of UTF-8 and will not do line ending conversion.

Implementation
dart
IOSink get nonBlocking {
  return _nonBlocking ??= new IOSink(new _FileStreamConsumer._fromStdio(_fd));
}

runtimeType no setter inherited

Type get runtimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
dart
external Type get runtimeType;

supportsAnsiEscapes no setter

bool get supportsAnsiEscapes

Whether connected to a terminal that supports ANSI escape sequences.

Not all terminals are recognized, and not all recognized terminals can report whether they support ANSI escape sequences, so this value is a best-effort attempt at detecting the support.

The actual escape sequence support may differ between terminals, with some terminals supporting more escape sequences than others, and some terminals even differing in behavior for the same escape sequence.

The ANSI color selection is generally supported.

Currently, a TERM environment variable containing the string xterm will be taken as evidence that ANSI escape sequences are supported. On Windows, only versions of Windows 10 after v.1511 ("TH2", OS build 10586) will be detected as supporting the output of ANSI escape sequences, and only versions after v.1607 ("Anniversary Update", OS build 14393) will be detected as supporting the input of ANSI escape sequences.

Implementation
dart
bool get supportsAnsiEscapes => _supportsAnsiEscapes(_fd);

terminalColumns no setter

int get terminalColumns

The number of columns of the terminal.

If no terminal is attached to stdout, a StdoutException is thrown. See hasTerminal for more info.

Implementation
dart
int get terminalColumns => _terminalColumns(_fd);

terminalLines no setter

int get terminalLines

The number of lines of the terminal.

If no terminal is attached to stdout, a StdoutException is thrown. See hasTerminal for more info.

Implementation
dart
int get terminalLines => _terminalLines(_fd);

Methods

add() inherited

void add(List<int> data)

Adds byte data to the target consumer, ignoring encoding.

The encoding does not apply to this method, and the data list is passed directly to the target consumer as a stream event.

This method must not be called when a stream is currently being added using addStream.

This operation is non-blocking. See flush or done for how to get any errors generated by this call.

The data list should not be modified after it has been passed to add because it is not defined whether the target consumer will receive the list in the original or modified state.

Individual values in data which are not in the range 0 .. 255 will be truncated to their low eight bits, as if by int.toUnsigned, before being used.

Inherited from _StdSink.

Implementation
dart
void add(List<int> data) {
  _lastWrittenCharIsCR = false;
  _sink.add(data);
}

addError() inherited

void addError(Object error, [StackTrace? stackTrace])

Passes the error to the target consumer as an error event.

This method must not be called when a stream is currently being added using addStream.

This operation is non-blocking. See flush or done for how to get any errors generated by this call.

Inherited from _StdSink.

Implementation
dart
void addError(error, [StackTrace? stackTrace]) {
  _sink.addError(error, stackTrace);
}

addStream() inherited

Future<dynamic> addStream(Stream<List<int>> stream)

Adds all elements of the given stream.

Returns a Future that completes when all elements of the given stream have been added.

If the stream contains an error, the addStream ends at the error, and the returned future completes with that error.

This method must not be called when a stream is currently being added using this method.

Individual values in the lists emitted by stream which are not in the range 0 .. 255 will be truncated to their low eight bits, as if by int.toUnsigned, before being used.

Inherited from _StdSink.

Implementation
dart
Future addStream(Stream<List<int>> stream) {
  _lastWrittenCharIsCR = false;
  return _sink.addStream(stream);
}

close() inherited

Future<dynamic> close()

Close the target consumer.

NOTE: Writes to the IOSink may be buffered, and may not be flushed by a call to close(). To flush all buffered writes, call flush() before calling close().

Inherited from _StdSink.

Implementation
dart
Future close() => _sink.close();

flush() inherited

Future<dynamic> flush()

Returns a Future that completes once all buffered data is accepted by the underlying StreamConsumer.

This method must not be called while an addStream is incomplete.

NOTE: This is not necessarily the same as the data being flushed by the operating system.

Inherited from _StdSink.

Implementation
dart
Future flush() => _sink.flush();

noSuchMethod() inherited

dynamic noSuchMethod(Invocation invocation)

Invoked when a nonexistent method or property is accessed.

A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:

dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time error

This invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).

Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.

A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:

dart
class MockList<T> implements List<T> {
  noSuchMethod(Invocation invocation) {
    log(invocation);
    super.noSuchMethod(invocation); // Will throw.
  }
}
void main() {
  MockList().add(42);
}

This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.

If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.

The default behavior is to throw a NoSuchMethodError.

Inherited from Object.

Implementation
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);

toString() inherited

String toString()

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Inherited from Object.

Implementation
dart
external String toString();

write() inherited

void write(Object? object)

Converts object to a String by invoking Object.toString and adds the encoding of the result to the target consumer.

This operation is non-blocking. See flush or done for how to get any errors generated by this call.

Inherited from _StdSink.

Implementation
dart
void write(Object? object) => _write(object);

writeAll() inherited

void writeAll(Iterable<dynamic> objects, [String sep = ""])

Iterates over the given objects and writes them in sequence.

If separator is provided, a write with the separator is performed between any two elements of objects.

This operation is non-blocking. See flush or done for how to get any errors generated by this call.

Inherited from _StdSink.

Implementation
dart
void writeAll(Iterable objects, [String sep = ""]) {
  Iterator iterator = objects.iterator;
  if (!iterator.moveNext()) return;
  if (sep.isEmpty) {
    do {
      _write(iterator.current);
    } while (iterator.moveNext());
  } else {
    _write(iterator.current);
    while (iterator.moveNext()) {
      _write(sep);
      _write(iterator.current);
    }
  }
}

writeCharCode() inherited

void writeCharCode(int charCode)

Writes the character of charCode.

This method is equivalent to write(String.fromCharCode(charCode)).

This operation is non-blocking. See flush or done for how to get any errors generated by this call.

Inherited from _StdSink.

Implementation
dart
void writeCharCode(int charCode) {
  if (!_windowsLineTerminator) {
    _sink.writeCharCode(charCode);
    return;
  }

  _write(String.fromCharCode(charCode));
}

writeln() inherited

void writeln([Object? object = ""])

Converts object to a String by invoking Object.toString and writes the result to this, followed by a newline.

This operation is non-blocking. See flush or done for how to get any errors generated by this call.

Inherited from _StdSink.

Implementation
dart
void writeln([Object? object = ""]) {
  if (!_windowsLineTerminator) {
    _sink.write('$object\n');
  } else {
    _sink.write(_convertToWindowsLineTerminators('$object\r\n'));
  }
}

Operators

operator ==() inherited

bool operator ==(Object other)

The equality operator.

The default behavior for all Objects is to return true if and only if this object and other are the same object.

Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:

  • Total: It must return a boolean for all arguments. It should never throw.

  • Reflexive: For all objects o, o == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must be true.

The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.

If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.

Inherited from Object.

Implementation
dart
external bool operator ==(Object other);