decodeProcessOutput function

String decodeProcessOutput(
  1. Object? raw
)

Decode raw process output as UTF-8, tolerating malformed byte sequences.

Process.run returns List<int> for stdout/stderr when invoked with stdoutEncoding: null / stderrEncoding: null; pass that here to decode it as UTF-8 regardless of the host's locale code page. Already-decoded String values pass through unchanged, and null becomes the empty string.

allowMalformed: true means bytes that are not valid UTF-8 (e.g. genuine OEM-encoded text from a legacy tool) degrade to the Unicode replacement character rather than throwing or producing double-mojibake.

Implementation

String decodeProcessOutput(Object? raw) {
  if (raw == null) return '';
  if (raw is String) return raw;
  if (raw is List<int>) return utf8.decode(raw, allowMalformed: true);
  return raw.toString();
}