Flutter 颤振-文本编辑控制器为空,但颤振认为存在';什么是价值礼物?
我正在尝试在我的web应用程序中实现“忘记密码”功能 我有一个电子邮件文本编辑控制器作为输入字段和一个提升的按钮,它最终会将值推送到我的forgotpassword函数 为了处理错误,我尝试实现一个if语句,以确保如果textfield为空,则不会发送请求 但是,当测试块时,Flatter似乎认为文本字段中有一个值,而我没有实际输入任何内容?有人能解释一下我错在哪里吗?谢谢这是我的密码:Flutter 颤振-文本编辑控制器为空,但颤振认为存在';什么是价值礼物?,flutter,dart,Flutter,Dart,我正在尝试在我的web应用程序中实现“忘记密码”功能 我有一个电子邮件文本编辑控制器作为输入字段和一个提升的按钮,它最终会将值推送到我的forgotpassword函数 为了处理错误,我尝试实现一个if语句,以确保如果textfield为空,则不会发送请求 但是,当测试块时,Flatter似乎认为文本字段中有一个值,而我没有实际输入任何内容?有人能解释一下我错在哪里吗?谢谢这是我的密码: class ForgotPasswordScreen extends StatelessWidget {
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
的实例,不为空。这就是为什么它会转到else
。在.text
上使用.isEmpty
,因为它返回字符串
if (forgotPasswordController.text.isNotEmpty) {
// pass
} else {
// fail
}