【发布时间】:2021-09-25 08:43:08
【问题描述】:
当我触摸 TextField 时,它会自动重新加载我的页面,我不能放一个字母
我尝试了很多方法,例如删除 setState 并且它有效,但我需要它来将我的值保存在数据中,我发现该方法可以将我的未来置于 initstate 但我不知道该怎么做,我'不确定这是解决方案。 我在 StateFulWidget 我也使用了 globalKey,但它在 build 方法之外。
这是我的代码:
class _UpdateProfileState extends State<UpdateProfile> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
...
//My future
body: Form(
key: _formKey,
child: Column(
children: <Widget>[
FutureBuilder<DocumentSnapshot>(
future: Users
.doc(uid)
.get(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
return displayUserInformation(context, snapshot);
} else {
return SizedBox(height: 400,
child: Center(child: CircularProgressIndicator()));
}
},
),
],
),
),
);
}
...
//And this is My textFormField
Padding(
padding: const EdgeInsets.all(16.0),
child: TextFormField(
onChanged: (value) =>
setState(() => _name = value.trim()),
initialValue: "${data['displayName'].toString()}",
cursorColor: DarkTurquoise,
decoration: InputDecoration(
labelText: 'Name',
labelStyle: TextStyle(
color: SeaTurquoise
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: SeaTurquoise, width: 2.0),),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: DarkGrey),
),
),
),
),
感谢您的回答
【问题讨论】:
-
尝试将“onChanged”替换为“onEditingComplete”
-
如果您在未来的构建器中使用文本字段,它将重新构建,您将无法使用它
-
当我用 onEditing Complete 替换 onChanged 时,它带有红色下划线,我在 Internet 上找不到 onEditingComplete 的方法
-
是的,我忘记了我在未来,但我必须找到一个解决方案来显示我的数据并对其进行编辑
-
你能展示完整的课程吗?
标签: firebase flutter dart future textformfield