首页 > 解决方案 > Flutter 将包含 \n 的 json 数据转换为新行

问题描述

我的 API 发送一个 json 格式如下:

{ status: false, message: 'This is an error \n\n with multi line!' }

我想显示消息并应用 json 中设置的新行。

我正在使用 Dio 包,并返回如下信息:

Response response = await Dio().post(...)
String message = response.data['message'];

这会按字面意思打印This is an error \n\n with multi line!而不是打印:

这是一个错误

多线!

在此处输入图像描述

如果我手动输入String message = 'This is an error \n\n with multi line!'效果很好。

在我看来,我必须使用replaceAll函数,但我不知道要替换什么。

标签: flutterdart

解决方案


尝试使用这个

replaceAll('\\n', '\n');

推荐阅读