FlutterBloc-将状态值设置为TextFormField中的初始文本
请查看我的示例代码:
@override
Widget build(BuildContext context) {
final selectedIssue = context.select((IssueDetailsBloc bloc) => bloc.state.selectedIssue) ?? InternalIssue();
return WillPopScope(
onWillPop: () async {
final issuesScheduleBloc = context.read<IssuesScheduleBloc>();
issuesScheduleBloc.add(UpdateIssueEvent(id: selectedIssue.id ?? 0, newData: selectedIssue));
return true;
},
child: Scaffold(
appBar: AppBar(title: const Text('Issue details')),
body: Column(
children: [
Text(selectedIssue.title ?? ''), //<==== correct value is displayed
TextFormField(initialValue: selectedIssue.title ?? '' //<==== selectedIssue.title is always null
, onChanged: (text) {
selectedIssue.title = text;
},)
],
),
),
);
}
我希望能够编辑我当前的问题值。要显示编辑的当前值,initialValue
属性已设置为selectionsue.title
。问题:只要selectedissue.title
用于显示静态文本,一切都很好。但是,每当我尝试将其用作文本表单字段的初始值时,值都会恢复为null。
有什么技巧如何正确执行?
Please look at my sample code:
@override
Widget build(BuildContext context) {
final selectedIssue = context.select((IssueDetailsBloc bloc) => bloc.state.selectedIssue) ?? InternalIssue();
return WillPopScope(
onWillPop: () async {
final issuesScheduleBloc = context.read<IssuesScheduleBloc>();
issuesScheduleBloc.add(UpdateIssueEvent(id: selectedIssue.id ?? 0, newData: selectedIssue));
return true;
},
child: Scaffold(
appBar: AppBar(title: const Text('Issue details')),
body: Column(
children: [
Text(selectedIssue.title ?? ''), //<==== correct value is displayed
TextFormField(initialValue: selectedIssue.title ?? '' //<==== selectedIssue.title is always null
, onChanged: (text) {
selectedIssue.title = text;
},)
],
),
),
);
}
I want to be able to edit my current issue value. To display current value for edit, initialValue
property has been set to selectedIssue.title
. Problem: as long as selectedIssue.title
is used to display static text, all is fine. But every time I try to use it as initial value for text form field, value is reverted to null.
Any tips how to do it correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在文件开头定义一个控制器,如下所示,为
您的文本字段提供此控制器,
然后在初始化状态下,为控制器提供如下文本
define a controller in the start of your file like this
give your text field this controller like this
then in your init state give the controller a text like this