flutter - 保存在 mysql 中的带有 \n 和 unicode 文字的文本在显示时不起作用
问题描述
我在 mysql 中存储了一个带有 \n 和 unicode 文字(如 \u2022)的文本字符串,然后在 Flutter 上使用 http api 调用检索它。使用 Text 小部件显示它时,这些转义符号不会按预期显示。当我直接传递字符串时,它可以工作。谁能帮帮我?
child: Column(
children: <Widget>[
Text(prompt.prompt_body, //This variable is from http call which does not work
textAlign: TextAlign.left,
style:TextStyle(
color: Colors.black,
fontSize: 13,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic
)),
Divider(),
Text("You live in a room in college which you share with another student.However, there are many problems with this arrangement and you find it very difficult to work.\n\nWrite a letter to the accommodation officer at the college. In the letter,\n\n \u2022 describe the situation\n \u2022 explain your problems and why it is difficult to work\n \u2022 say what kind of accommodation you would prefer", //this part works
textAlign: TextAlign.left,
style:TextStyle(
color: Colors.black,
fontSize: 13,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic
))
],
),
为了响应 Gunter 的询问,我在 api 调用中添加了以下代码:
class PromptModel {
int id;
String prompt_body;
String prompt_image;
PromptModel(this.id, this.prompt_body, this.prompt_image);
PromptModel.fromJson(Map<String, dynamic> parsedJson) {
id = parsedJson['id'];
prompt_body = parsedJson['prompt_body'];
prompt_image = parsedJson['prompt_image'];
}
}
....
class PromptListPageState extends State<PromptListPage> {
int counter = 0;
List<PromptModel> prompts = [];
void fetchImage() async {
counter++;
var response =
await get('http://10.0.2.2:8080/TestPrompt');
var promptModel = PromptModel.fromJson(json.decode(response.body));
setState(() {
prompts.add(promptModel);
});
}
以下是api调用的响应:
{"id":1,"prompt_body":"You live in a room in college which you share with another student.However, there are many problems with this arrangement and you find it very difficult to work.\\n\\nWrite a letter to the accommodation officer at the college. In the letter,\\n\\n \\u2022 describe the situation\\n \\u2022 explain your problems and why it is difficult to work\\n \\u2022 say what kind of accommodation you would prefer","prompt_image":"http://10.0.2.2:8080/test.jpg"}
解决方案
我通过使用 TextFormField 从颤振中输入字符串解决了这个问题。直接在数据库端插入文本很棘手。代码如下:
Widget build(context) {
return MaterialApp(
home: Scaffold(
body: Form(
key: formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
controller: myController,
maxLines: 5,
validator: (val) =>
(val == null || val.isEmpty) ? "请输入商品名称" : null,
decoration: const InputDecoration(
//icon: Icon(Icons.person),
hintText: 'add the prompt here:',
labelText: 'Prompt content',
border: OutlineInputBorder(
borderSide: BorderSide(color: Colors.teal)),
),
onSaved: (val) => this.content = val,
),
new Container(
margin: const EdgeInsets.only(top: 10.0),
child: new RaisedButton(
onPressed: _save,
child: new Text('Save'),
),
)
]),
),
appBar: AppBar(
title: Text('Add Essay Prompt'),
),
),
);
}
}
推荐阅读
- python - JSONDecodeError:期望值
- php - 在 HTML 中运行 php
- curl - “* Connection #0 to host example.com保持不变”是什么意思?
- php - Pusher 不从后端发送任何信号
- android - LinearLayout 不在 ScrollView 内滚动
- python - 如何在 df Pandas Python 中更改元素的值
- c - 检查输入缓冲区 ncurses 中等待的项目数
- selenium - Selenium 获取 href 链接返回 null
- node-postgres - 枚举数组的参数化?
- python - Discord.py Cogs 错误 FileNotFoundError:[Errno 2] 没有这样的文件或目录:'./cogs'