我正在尝试在Web应用程序中实现“忘记密码”功能。
我有一个电子邮件文本编辑控制器作为输入字段,还有一个提升的按钮,该按钮最终会将值推入我的忘记密码功能。
为了处理错误,我尝试实现一个if语句,以确保如果文本字段为空,则不发送请求。
但是,在测试块时,Flutter似乎认为文本字段中存在一个值,而我没有实际输入任何内容?有人可以解释我要去哪里错吗?谢谢。这是我的代码:
class ForgotPasswordScreen extends StatelessWidget {
final TextEditingController forgotPasswordController =
TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Forgot Password'),
),
body: Center(
child: Container(
width: MediaQuery.of(context).size.width * 0.5,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
controller: forgotPasswordController,
decoration: InputDecoration(
hintStyle: TextStyle(color: Colors.grey),
border: OutlineInputBorder()),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
try {
if (forgotPasswordController.value != null) {
print('Success');
} else {
print("fail");
}
} catch (e) {
print(e);
}
},
child: Text('Send reset link')),
)
],
),
),
));
}
}
默认情况下,值为TextEditingController
,而不是null
。
.value
不是字符串。这是TextEditingValue
的实例,不能为null。这就是为什么else
。使用.isEmpty
on,.text
因为它返回String。
if (forgotPasswordController.text.isNotEmpty) {
// pass
} else {
// fail
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句