Writing endlines to files in with dart:io

Issue

I am using WebStorm on Windows and I have simple command-line dart app:

import 'dart:io';

main(List<String> arguments) {
  File testFile = new File("test.txt");
  testFile.writeAsStringSync("AAA\nBBBBB\rCCCCC");
}

Program executes successfully, however the .txt file opened via Notepad appears to contain

AAABBBBBCCCCC

however if I try to past the content to other environments (WebStorm, StackOverflow question textarea), the text appears as

AAA
BBBBB
CCCCC

I feel like I am missing something basic … any idea?

Solution

Windows uses \r\n by default for line endings.

Not all applications treat \r or \n as line ending on windows.
This behavior is not related to Dart in any way, that’s just how some applications interpret these codes.

Answered By – Günter Zöchbauer

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *