除非硬编码字符串,否则不会打破线条

问题描述 投票:2回答:1

我可以看到Flutter允许我在字符串中使用“\ n \ n”,它会导致换行符出现在Text项目中:

final String answer = "This is my text.\n\n"
    "Here is the 2nd line.";

这是我的文字。

这是第二行。

但是,当我尝试使用从firebase中提取的内容并在变量中设置时,实际打印换行符(“\ n”):

final String answer = faq['answer'];

显示:

这是我的文字。\ n \ n这是第二行。

如何让我的“\ n \ n”实际显示为换行符?

flutter line-breaks
1个回答
4
投票

Firestore不支持字符串值中的任何转义序列。如果你在一个字符串中写“\ n”,那么当你阅读它时,你会得到完全的回复。

所以你可以尝试这样的事情:

final String answer = (faq['answer'] as String).replaceAll("\\n", "\n");
© www.soinside.com 2019 - 2024. All rights reserved.