'Newline character \n Not Working While Loading Text From Firebase

Hi i am using some text data to load from firebase and then showing it as Text.I am using Text() widget .I have a field called description in my firebase document which for example contains info like this:

description: This is \n some text.

So as you can see there should be two lines first containing This is and second line containing some text.But this does not happen.All the text is shown in one line only. Also note that this only happens when i read the text from firebase document and but when i replace it with custom text(string) containing \n which is not read from firebase it works fine. Why is this happening and how can i resolve this?Why does it not work when loading string from firestore.



Solution 1:[1]

I faced the same problem. Just run the text through this in Flutter to fix the problem.

  String convertNewLine(String content) {
      print("Converting");
      return content.replaceAll(r'\n', '\n');
  }

Solution 2:[2]

You simply have to use a multiline string. Give a look at the documentation for more info.

Text('''Hello\nWorld''')

This would be printed as

Hello
World

Basically when using triple quotes, you can use \n for a new line.

Solution 3:[3]

A late answer but might be useful for some people, firebase adds its own "" to make the special character you wrote a string, so simply the "\n" you typed in your firebase records, firebase changes it to "\n" in your code, instead of content.replaceAll(r'\n', '\n'); writing content.replaceAll('\\n', '\n'); will do the job. but i would prefer to do it like content.replaceAll('\\', '\'); to it solves all related issues.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bearious
Solution 2 Alberto Miola
Solution 3 Abdallah A. Odeh