Flutter \n\n 除非硬编码字符串,否则不会断行
Flutter \n\n not breaking lines unless hard-coded string
我可以看到 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”,它会导致在 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");