readTextTolerant function

String readTextTolerant(
  1. String path
)

Reads a text file that may contain invalid UTF-8 (e.g. logs.txt, which carries raw app and tool output). Malformed sequences become U+FFFD instead of throwing. Returns an empty string when the file does not exist.

Implementation

String readTextTolerant(String path) {
  final file = File(path);
  if (!file.existsSync()) return '';
  return tolerantUtf8.decode(file.readAsBytesSync());
}