Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 方法';验证';被调用为空_Flutter_Dart - Fatal编程技术网

Flutter 方法';验证';被调用为空

Flutter 方法';验证';被调用为空,flutter,dart,Flutter,Dart,方法validate正在调用null 我怎样才能修好它 这是我的代码 saveUserInfoToFireStore() async { final GoogleSignInAccount gCurrentUser = gSignIn.currentUser; DocumentSnapshot documentSnapshot = await userRefrence.document(gCurrentUser.id).get(); if (!docu

方法
validate
正在调用null

我怎样才能修好它

这是我的代码

  saveUserInfoToFireStore() async {
    final GoogleSignInAccount gCurrentUser = gSignIn.currentUser;
    DocumentSnapshot documentSnapshot =
        await userRefrence.document(gCurrentUser.id).get();
    if (!documentSnapshot.exists) {
      final username = await Navigator.push(context,
          MaterialPageRoute(builder: (context) => CreateAccountPage()));}
CreateAccountPage.dart

class _CreateAccountPageState extends State<CreateAccountPage> {
  final _scaffoldKey = GlobalKey<ScaffoldState>();
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  // final _formKey = GlobalKey<FormState>();
  String username;

  submitUsername() {
    final form = _formKey.currentState;
    if (form.validate()) {
      form.save();
      SnackBar snackBar = SnackBar(
        content: Text("Welcome" + username),
      );
      _scaffoldKey.currentState.showSnackBar(snackBar);
      Timer(Duration(seconds: 4), () {
        Navigator.pop(context, username);
      });
    }
  }

  @override
  Widget build(BuildContext parentContext) {
    return Scaffold(
      key: _scaffoldKey,
      appBar:
          header(context, strTitle: "Settings", disappearedBackButton: true),
      body: ListView(
        children: <Widget>[
          Container(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(top: 26.0),
                  child: Center(
                    child: Text(
                      "Set up  a Username",
                      style: TextStyle(fontSize: 26.0),
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.all(17.0),
                  child: Container(
                    child: Form(
                      key: _formKey,
                      autovalidate: true,
                      child: TextFormField(
                        style: TextStyle(color: Colors.white),
                        validator: (val) {
                          if (val.trim().length < 5 || val.isEmpty) {
                            return "user name is very Short";
                          } else if (val.trim().length < 10 || val.isEmpty) {
                            return "user name is very long";
                          } else {
                            return null;
                          }
                        },
                        onSaved: (val) => username = val,
                        decoration: InputDecoration(
                          enabledBorder: UnderlineInputBorder(
                            borderSide: BorderSide(color: Colors.grey),
                          ),
                          focusedBorder: UnderlineInputBorder(
                            borderSide: BorderSide(color: Colors.white),
                          ),
                          border: OutlineInputBorder(),
                          labelText: "Username",
                          labelStyle: TextStyle(fontSize: 16.0),
                          hintText: "must be atleast 5 charecters",
                          hintStyle: TextStyle(color: Colors.grey),
                        ),
                      ),
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: submitUsername(),
                  child: Container(
                    height: 55.0,
                    width: 360.0,
                    decoration: BoxDecoration(
                      color: Colors.lightGreen,
                      borderRadius: BorderRadius.circular(8.0),
                    ),
                    child: Center(
                      child: Text(
                        "Proceed",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.0,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
class\u CreateAccountPageState扩展状态{
最终_scaffoldKey=GlobalKey();
最终的GlobalKey _formKey=GlobalKey();
//final _formKey=GlobalKey();
字符串用户名;
submitUsername(){
最终形式=_formKey.currentState;
if(form.validate()){
form.save();
SnackBar SnackBar=SnackBar(
内容:文本(“欢迎”+用户名),
);
_scaffoldKey.currentState.showSnackBar(snackBar);
计时器(持续时间(秒数:4),(){
pop(上下文、用户名);
});
}
}
@凌驾
小部件构建(BuildContext parentContext){
返回脚手架(
钥匙:_scaffoldKey,
appBar:
标题(上下文,标题:“设置”,消失BackButton:true),
正文:ListView(
儿童:[
容器(
子:列(
儿童:[
填充物(
填充:仅限边缘设置(顶部:26.0),
儿童:中心(
子:文本(
“设置用户名”,
样式:TextStyle(fontSize:26.0),
),
),
),
填充物(
填充:所有边缘设置(17.0),
子:容器(
孩子:表格(
键:_formKey,
自动验证:true,
子项:TextFormField(
样式:TextStyle(颜色:Colors.white),
验证器:(val){
if(val.trim().长度<5 | | val.isEmpty){
返回“用户名很短”;
}否则如果(val.trim().长度<10 | | val.isEmpty){
返回“用户名很长”;
}否则{
返回null;
}
},
onSaved:(val)=>username=val,
装饰:输入装饰(
enabledBorder:UnderlineInputBorder(
borderSide:borderSide(颜色:Colors.grey),
),
FocusedOrder:下划线输入边框(
borderSide:borderSide(颜色:Colors.white),
),
边框:OutlineInputBorder(),
labelText:“用户名”,
标签样式:文本样式(字体大小:16.0),
hintText:“必须至少有5个字符”,
hintStyle:TextStyle(颜色:Colors.grey),
),
),
),
),
),
手势检测器(
onTap:submitUsername(),
子:容器(
身高:55.0,
宽度:360.0,
装饰:盒子装饰(
颜色:颜色。浅绿色,
边界半径:边界半径。圆形(8.0),
),
儿童:中心(
子:文本(
“继续”,
样式:TextStyle(
颜色:颜色,白色,
字体大小:16.0,
fontWeight:fontWeight.bold,
),
),
),
),
),
],
),
)
],
),
);
}
}

问题在于您的
onTap
功能

submitUsername
内部的
onPressed
是一个函数引用,这意味着它不会立即执行。它在用户单击
按钮后执行

submitUsername()
是一个函数调用,它会立即执行


  • 您应该使用
    submitUsername
    而不是
    submitUsername()
  • 手势检测器(
    onTap:submitUsername,//新行
    子:容器(
    身高:55.0,
    宽度:360.0,
    装饰:盒子装饰(
    颜色:颜色。浅绿色,
    边界半径:边界半径。圆形(8.0),
    ),
    儿童:中心(
    子:文本(
    “继续”,
    样式:TextStyle(
    颜色:颜色,白色,
    字体大小:16.0,
    fontWeight:fontWeight.bold,
    ),
    ),
    ),
    ),
    ),
    

  • 在将用作回调的匿名函数中调用
    submitUsername
    函数
  • 手势检测器(
    onTap:(){
    submitUsername();
    },
    子:容器(
    身高:55.0,
    宽度:360.0,
    装饰:盒子装饰(
    颜色:颜色。浅绿色,
    边界半径:边界半径。圆形(8.0),
    ),
    儿童:中心(
    子:文本(
    “继续”,
    样式:TextStyle(
    颜色:颜色,白色,
    字体大小:16.0,
    fontWeight:fontWeight.bold,
    ),
    ),
    ),
    ),
    ),