Firebase Firestore新行命令 [英] Firebase Firestore new line command

查看:44
本文介绍了Firebase Firestore新行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新行命令在文本中创建新行.从此问题看来,这是不可能的:换行命令(\ n)无法与Firebase Firestore数据库字符串一起使用

I'm trying to utilize a new line command to create a new line in text. It appears from this issue that this is not possible: New Line Command (\n) Not Working With Firebase Firestore Database Strings

是否仍然如此?假设是这样,Flutter是否提供与以下功能相似的任何方法,让我可以解决此问题?

Is this still the case? Assuming so, does Flutter offer any methods with similar functionality to the following that would allow me to work around this?

label.text = stringRecived.replacingOccurrences(of: "\n", with: "\n")

*更多上下文: 这是我输入数据的Firestore字符串的图片.

*More context: Here is a picture of my Firestore string where I entered the data.

然后我使用将来的构建器从Firestore调用此函数,然后将字符串(在本例中为注释)传递到另一个小部件中.

I then use a future builder to call this from Firestore and then pass the string (in this case comment) into another widget.

return new SocialFeedWidget(
  filter: false,
  articleID: document['article'],
  article_header: document['article_title'],
  userName: document['author'],
  spectrumValue: document['spectrum_value'].toDouble(),
  comment: document['comment'],
  fullName: document['firstName'] + " " + document['lastName'],
  user_id: document['user_id'],
  postID: document.documentID,
  posterID: userID,
  );
}).toList(),

在新的小部件中,我将其作为字符串传递,通过文本小部件传递给它,如下所示:

In the new widget, I pass it in as a string an feed it via a Text widget as follows:

new Text(
  comment,
  style: new TextStyle(
    color: Color.fromRGBO(74, 74, 74, 1.0),
    fontSize: 13.0,
  ),
),

当它出现时,它仍然在字符串中包含\ n.

When it appears, it still has the \n within the string.

推荐答案

在大多数编程语言中,如果在字符串中包含\n,它将两个字符解释为一个(转义)序列,并且实际上存储了一个非ASCII码为10的可打印字符.

In most programming languages if you include \n in a string, it interprets the two characters as a (escape) sequence and it actually stores a single non-printable character with ASCII code 10.

如果您在Firestore控制台中从字面上键入\n到文档中,它将在字符串中存储该确切的字面值.这就是两个字符\n.

If you literally type \n into a document in the Firestore console, it stores that exact literal value in the string. So that's two characters \ and n.

这两个不一样.实际上,如果您想在大多数编程语言中以字符串形式输入两个字符的序列,则最终会以"\\n"结尾.这是两个反斜杠:第一个反斜杠开始转义序列,第二个反斜杠表示它是文字\,然后是文字n.

These two are not the same. In fact, if you'd want to enter the two-character sequence in a string in most programming languages you'd end up with "\\n". That's two backslashes: the first to start an escape sequence, the second to indicate it's a literal \, and then a literal n.

因此在Flutter中,如果您将字面意义上的两个字符\n存储在字符串中,并且想要将其显示为换行符,则需要将字符串解码回一个换行符.幸运的是,这很简单:

So in Flutter, if you've stored the literal two-characters \n in a string and you want to display it as a newline, you need to decode the string back into a single line break character. This is luckily quite simple with:

yourString.replaceAll("\\n", "\n");

例如,这就是我刚刚在应用程序中测试过的内容.我在Firestore控制台中的文档显示:

For example, this is what I just tested in an app. My document in the Firestore console shows:

然后是我的代码:

var doc = await Firestore.instance.collection("weather").document("sf").get();
var weather = doc.data["condition"]
print(weather);
print(weather.replaceAll("\\n", "\n"));

第一个print语句输出:

Sunny\nI think

第二次打印时:

Sunny

I think


有趣的事实:当我在Flutter中运行此代码时:


Fun fact: when I run this code in Flutter:

Firestore.instance.collection("weather").document("test").setData({ 'condition': "nice\nand\nsunny" });

它在Firestore控制台中显示如下:

It shows up like this in the Firestore console:

因此,字符串中不可打印的\n看起来在控制台中显示为空格.我还没有找到在Firestore控制台中将换行符输入到字符串中的方法.

So it looks like the unprintable \n in the string shows up as a space in the console. I haven't found a way yet to enter a newline into a string in the Firestore console.

这篇关于Firebase Firestore新行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆