简体   繁体   中英

NoSuchMethodError: The method 'validate' was called on null ...... flutter-Firebase

hello Im a beginer in Flutter and here is the problem- im trying to build a signup form with validation and 'signup' button which must show if the data is valid or no but there is 'NoSuchMethodError: The method 'validate' was called on null' in debuger when i press this 'signup' button. Here is the code:

import 'package:flutter/material.dart';
import 'package:flutter_auth/Screens/Login/login_screen.dart';
import 'package:flutter_auth/Screens/Signup/components/background.dart';
import 'package:flutter_auth/Screens/Signup/components/or_divider.dart';
import 'package:flutter_auth/Screens/Signup/components/social_icon.dart';
import 'package:flutter_auth/Screens/Signup/signup_screen.dart';
import 'package:flutter_auth/components/already_have_an_account_acheck.dart';
import 'package:flutter_auth/components/rounded_button.dart';
import 'package:flutter_auth/components/rounded_input_field.dart';
import 'package:flutter_auth/components/rounded_password_field.dart';
import 'package:flutter_svg/svg.dart';
import 'package:firebase_auth/firebase_auth.dart';

class Body extends StatefulWidget {
  Body({Key key}) : super(key: key);
  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  var myemail, mypassword;
  GlobalKey<FormState> formstate = new GlobalKey<FormState>();
  body() async {
    var formdata = formstate.currentState;
    if (formdata.validate()) {
      print("============================================");
      print("valid");
    } else {
      print("============================================");
      print("not valid");
    }
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Background(
      child: SingleChildScrollView(
        child: Container(
          alignment: Alignment.center,
          child: Form(
              key: formstate,
              child: Column(
                children: [
                  Text(
                    "SIGNUP",
                    style: TextStyle(fontWeight: FontWeight.bold),
                  ),
                  SizedBox(height: size.height * 0.03),
                  SvgPicture.asset(
                    "assets/icons/signup.svg",
                    height: size.height * 0.35,
                  ),
                  RoundedInputField(
                    /*********************************email************************/
                    hintText: "Your Email",
                    onChanged: (value) {
                      myemail = value;
                      if (value.length > 100) {
                        return "email can't be larger than 100 letter";
                      }
                      if (value.length < 2) {
                        return "email can't be less than 2 letter";
                      }
                      return null;
                    },
                  ),
                  RoundedPasswordField(
                    /*********************************password************************/
                    onChanged: (value) {
                      mypassword = value;
                      if (value.length > 100) {
                        return "password can't be larger than 100 letter";
                      }
                      if (value.length < 4) {
                        return "Password can't be less than 4 letter";
                      }
                      return null;
                    },
                  ),
                  RoundedButton(
                    /*********************************Button************************/
                    text: "SIGNUP",
                    press: () async {
                      print("there is the prooooooooob");
                      return await body();
                    },
                  ),
                  SizedBox(height: size.height * 0.03),
                  AlreadyHaveAnAccountCheck(
                    login: false,
                    press: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) {
                            return LoginScreen();
                          },
                        ),
                      );
                    },
                  ),
                  OrDivider(),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      SocalIcon(
                        iconSrc: "assets/icons/facebook.svg",
                        press: () {},
                      ),
                      SocalIcon(
                        iconSrc: "assets/icons/twitter.svg",
                        press: () {},
                      ),
                      SocalIcon(
                        iconSrc: "assets/icons/google-plus.svg",
                        press: () {},
                      ),
                    ],
                  )
                ],
              )),
        ),
      ),
    );
  }
}

You don't have any validator s in input fields.

Main idea behind validate() is that it calls all validator s from the fields which are members of the Form with the key of formstate (in your case).

在此处输入图像描述

How does this work url

Full form creating url

i am the same problem but solved because i declare variable wrong. Make sure you declare correctly the variable.

final _formKey = GlobalKey<FormState>(); //Correct
final GlobalKey _formKey = GlobalKey<FormState>(); //Wrong
GlobalKey _formKey = GlobalKey<FormState>(); //wrong

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM