我可以看到 Flutter 允许我在字符串中使用“\n\n”,它会导致 Text
项中出现换行符:
final String answer = "This is my text.\n\n"
"Here is the 2nd line.";
This is my text.
Here is the 2nd line.
但是,当我尝试使用从 firebase 提取并设置在变量中的内容时,实际上会打印换行符(“\n”):
final String answer = faq['answer'];
演出:
This is my text.\n\nHere is the 2nd line.
如何让我的“\n\n”实际显示为换行符?
最佳答案
Firestore 不支持字符串值中的任何转义序列。如果你在一个字符串中写“\n”,当你阅读它时,你会得到完全正确的结果。
所以你可以试试这样的:
final String answer = (faq['answer'] as String).replaceAll("\\n", "\n");
关于Flutter\n\n 除非硬编码字符串,否则不会换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55564786/