Flutter Fluter对null调用了验证方法
我有以下NoSuchMethodError被抛出建筑,但我不知道错误来自哪里Flutter Fluter对null调用了验证方法,flutter,Flutter,我有以下NoSuchMethodError被抛出建筑,但我不知道错误来自哪里 Form( key: formKeyEmail, child: Padding( padding: EdgeInsets.only( top: MediaQuery.of(context).size.height * 0.03, left: MediaQ
Form(
key: formKeyEmail,
child: Padding(
padding: EdgeInsets.only(
top: MediaQuery.of(context).size.height * 0.03,
left: MediaQuery.of(context).size.width * 0.05,
right: MediaQuery.of(context).size.width * 0.05),
child: TextFormField(
key: formKeyEmail,
autovalidate: isemail,
validator: (val) {
if (!EmailValidator.validate(val)) {
return "You have error in email";
}
},
onTap: () {
setState(() {
isemail = true;
dropdownforgotpassword = 0;
if (isemail) dropdownforgotpassword = 1;
if (ispassword) dropdownforgotpassword++;
});
},
onChanged: (String val) {
if (isemail) _email = val;
},
decoration: InputDecoration(
hintText: 'Email : email@domain.com',
isDense: true,
prefixIcon: Icon(Icons.email),
hintStyle: TextStyle(fontSize: 12),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(30.0))),
),
),
),
),
Form(
key: formKeyPassword,
child: Padding(
padding: EdgeInsets.only(
top: MediaQuery.of(context).size.height * 0.03,
left: MediaQuery.of(context).size.width * 0.05,
right: MediaQuery.of(context).size.width * 0.05),
child: TextFormField(
key: formKeyPassword,
autovalidate: ispassword,
obscureText: _obs,
onTap: () {
setState(() {
ispassword = true;
dropdownforgotpassword = 0;
if (isemail) dropdownforgotpassword = 1;
if (ispassword) dropdownforgotpassword++;
});
},
onChanged: (String val) {
if (ispassword) _password = val;
},
validator: (val) {
if (val.length < 7) {
return 'Password is too short';
}
},
decoration: InputDecoration(
hintText: 'Passowrd : ***************',
hintStyle: TextStyle(fontSize: 12),
contentPadding: EdgeInsets.only(),
prefixIcon: GestureDetector(
onTap: () {
setState(() {
_obs = !_obs;
});
},
child: Icon(
Icons.visibility_off,
size: MediaQuery.of(context).size.width * 0.06,
),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(30.0))),
),
),
),
),
表单(
关键字:formKeyEmail,
孩子:填充(
填充:仅限边缘设置(
顶部:MediaQuery.of(上下文).size.height*0.03,
左:MediaQuery.of(context).size.width*0.05,
右:MediaQuery.of(context).size.width*0.05),
子项:TextFormField(
关键字:formKeyEmail,
自动验证:电子邮件,
验证器:(val){
如果(!EmailValidator.validate(val)){
返回“您在电子邮件中有错误”;
}
},
onTap:(){
设置状态(){
isemail=true;
dropdownforgotpassword=0;
如果(isemail)dropdownforgotpassword=1;
如果(ispassword)下拉放弃密码++;
});
},
onChanged:(字符串val){
如果(isemail)_email=val;
},
装饰:输入装饰(
hintText:'电子邮件:email@domain.com',
是的,
前缀:图标(Icons.email),
hintStyle:TextStyle(字体大小:12),
边框:大纲输入边框(
borderRadius:borderRadius.all(半径.圆形(30.0)),
),
),
),
),
形式(
密钥:formKeyPassword,
孩子:填充(
填充:仅限边缘设置(
顶部:MediaQuery.of(上下文).size.height*0.03,
左:MediaQuery.of(context).size.width*0.05,
右:MediaQuery.of(context).size.width*0.05),
子项:TextFormField(
密钥:formKeyPassword,
自动验证:ispassword,
模糊文本:_obs,
onTap:(){
设置状态(){
ispassword=true;
dropdownforgotpassword=0;
如果(isemail)dropdownforgotpassword=1;
如果(ispassword)下拉放弃密码++;
});
},
onChanged:(字符串val){
如果(ispassword)_password=val;
},
验证器:(val){
如果(值长度<7){
返回“密码太短”;
}
},
装饰:输入装饰(
hintText:'Passowrd:***************',
hintStyle:TextStyle(字体大小:12),
contentPadding:EdgeInsets.only(),
前缀:手势检测器(
onTap:(){
设置状态(){
_obs=!\u obs;
});
},
子:图标(
Icons.visibility\u关闭,
大小:MediaQuery.of(上下文)。size.width*0.06,
),
),
边框:大纲输入边框(
borderRadius:borderRadius.all(半径.圆形(30.0)),
),
),
),
),
您可以复制粘贴运行下面的完整代码请删除
TextFormField
中的key:formkeymail
和key:formKeyPassword
代码片段
child: TextFormField(
//key: formKeyEmail,
...
child: TextFormField(
//key: formKeyPassword,
工作演示
完整代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
final formKeyEmail = GlobalKey<FormState>();
final formKeyPassword = GlobalKey<FormState>();
bool isemail = false;
bool ispassword = false;
String _email = "";
String _password = "";
bool _obs = true;
int dropdownforgotpassword = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Form(
key: formKeyEmail,
child: Padding(
padding: EdgeInsets.only(
top: MediaQuery.of(context).size.height * 0.03,
left: MediaQuery.of(context).size.width * 0.05,
right: MediaQuery.of(context).size.width * 0.05),
child: TextFormField(
//key: formKeyEmail,
autovalidate: isemail,
validator: (val) {
/*if (!EmailValidator.validate(val)) {
return "You have error in email";
}*/
},
onTap: () {
setState(() {
isemail = true;
dropdownforgotpassword = 0;
if (isemail) dropdownforgotpassword = 1;
if (ispassword) dropdownforgotpassword++;
});
},
onChanged: (String val) {
if (isemail) _email = val;
},
decoration: InputDecoration(
hintText: 'Email : email@domain.com',
isDense: true,
prefixIcon: Icon(Icons.email),
hintStyle: TextStyle(fontSize: 12),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(30.0))),
),
),
),
),
Form(
key: formKeyPassword,
child: Padding(
padding: EdgeInsets.only(
top: MediaQuery.of(context).size.height * 0.03,
left: MediaQuery.of(context).size.width * 0.05,
right: MediaQuery.of(context).size.width * 0.05),
child: TextFormField(
//key: formKeyPassword,
autovalidate: ispassword,
obscureText: _obs,
onTap: () {
setState(() {
ispassword = true;
dropdownforgotpassword = 0;
if (isemail) dropdownforgotpassword = 1;
if (ispassword) dropdownforgotpassword++;
});
},
onChanged: (String val) {
if (ispassword) _password = val;
},
validator: (val) {
if (val.length < 7) {
return 'Password is too short';
}
},
decoration: InputDecoration(
hintText: 'Passowrd : ***************',
hintStyle: TextStyle(fontSize: 12),
contentPadding: EdgeInsets.only(),
prefixIcon: GestureDetector(
onTap: () {
setState(() {
_obs = !_obs;
});
},
child: Icon(
Icons.visibility_off,
size: MediaQuery.of(context).size.width * 0.06,
),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(30.0))),
),
),
),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
final formKeyEmail=GlobalKey();
final formKeyPassword=GlobalKey();
bool-isemail=false;
bool ispassword=false;
字符串_email=“”;
字符串_password=“”;
bool_obs=真;
int dropdownforgotpassword=0;
void _incrementCounter(){
设置状态(){
_计数器++;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
形式(
关键字:formKeyEmail,
孩子:填充(
填充:仅限边缘设置(
顶部:MediaQuery.of(上下文).size.height*0.03,
左:MediaQuery.of(context).size.width*0.05,
右:MediaQuery.of(context).size.width*0.05),
子项:TextFormField(
//关键字:formKeyEmail,
自动验证:电子邮件,
验证器:(val){
/*如果(!EmailValidator.validate(val)){
返回“您在电子邮件中有错误”;
}*/
},
onTap:(){
设置状态(){
isemail=true;
dropdownforgotpassword=0;
如果(isemail)dropdownforgotpassword=1;
如果(ispassword)下拉放弃密码++;
});
},