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

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

问题描述

我正在尝试使用换行命令在文本中创建新行.从这个问题看来,这是不可能的:新行命令 ( ) 不适用于 Firebase Firestore 数据库字符串

还是这样吗?假设是这样,Flutter 是否提供了与以下功能类似的任何方法来解决这个问题?

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

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

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

返回新的SocialFeedWidget(过滤器:假,文章ID:文档['文章'],article_header:文档['article_title'],用户名:文档['作者'],频谱值:文档['spectrum_value'].toDouble(),评论:文档['评论'],fullName: 文档['firstName'] + " " + 文档['lastName'],user_id: 文档['user_id'],postID:document.documentID,海报ID:用户ID,);}).toList(),

在新的小部件中,我将其作为字符串传入并通过文本小部件提供,如下所示:

新文本(评论,样式:新文本样式(颜色:Color.fromRGBO(74, 74, 74, 1.0),字体大小:13.0,),),

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

解决方案

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

如果您直接在 Firestore 控制台的文档中键入 ,它会将确切的文字值存储在字符串中.所以这是两个字符 n.

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

因此在 Flutter 中,如果您已将文字两个字符 存储在一个字符串中,并且希望将其显示为换行符,则需要将该字符串解码回一行打破字符.幸运的是,这很简单:

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

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

Firestore 控制台中文档中的文字两字符 
 序列

然后是我的代码:

var doc = await Firestore.instance.collection("weather").document("sf").get();var 天气 = doc.data[条件"]打印(天气);打印(weather.replaceAll(\n",
"));

第一个 print 语句,打印:

<块引用>

阳光 我想

虽然第二次打印:

<块引用>

晴天

<块引用>

我认为


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

Firestore.instance.collection("weather").document("test").setData({ 'condition': "nice
and
sunny" });

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

所以看起来字符串中不可打印的 在控制台中显示为一个空格.我还没有找到在 Firestore 控制台中将换行符输入到字符串中的方法.不过,API 会正确检索换行符,因此这只会影响 Firebase 控制台.

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 ( ) Not Working With Firebase Firestore Database Strings

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: "
", with: "
")

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

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,
  ),
),

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

解决方案

In most programming languages if you include 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.

If you literally type into a document in the Firestore console, it stores that exact literal value in the string. So that's two characters and 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.

So in Flutter, if you've stored the literal two-characters 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", "
");

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

Literal two-character 
 sequence in a document in the Firestore console

And then my code:

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

That first print statement, prints:

Sunny I think

While the second prints:

Sunny

I think


Fun fact: when I run this code in Flutter:

Firestore.instance.collection("weather").document("test").setData({ 'condition': "nice
and
sunny" });

It shows up like this in the Firestore console:

So it looks like the unprintable 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. The API retrieves the line breaks correctly though, so this only affects the Firebase console.

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

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