skip to Main Content
ElevatedButton(
                  onPressed: () async {
                    if (_formKey.currentState!.validate()) {
                      try {
                        final newUser =
                            await _auth.createUserWithEmailAndPassword(
                                email: email.text, password: password.text);
                        if (newUser != null) {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => DashboardScreen(),
                              ));
                        }

                        setState(() {});
                      } catch (e) {
                        print(e);
                      }
                    }
                  },
                  child: const Text(tSignup)),

Code for the form:

Form(
        child: Column(
          key: _formKey,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            TextFormField(
              controller: fullName,
              validator: (value) {
                if (value == null || value.isEmpty) return 'Field is required.';
                return null;
              },
              decoration: const InputDecoration(
                  label: Text(tFullName),
                  prefixIcon: Icon(Icons.person_outline_rounded),
                  border: OutlineInputBorder(),
                  labelStyle: TextStyle(color: tSecondaryColor),
                  focusedBorder: OutlineInputBorder(
                      borderSide:
                          BorderSide(width: 2.0, color: tSecondaryColor))),
            ),
            const SizedBox(
              height: 10,
            ),
            TextFormField(
              controller: email,
              validator: (value) {
                if (value == null || value.isEmpty) return 'Field is required.';
                return null;
              },
              decoration: const InputDecoration(
                  label: Text(tEmail),
                  prefixIcon: Icon(Icons.person_outline_rounded),
                  border: OutlineInputBorder(),
                  labelStyle: TextStyle(color: tSecondaryColor),
                  focusedBorder: OutlineInputBorder(
                      borderSide:
                          BorderSide(width: 2.0, color: tSecondaryColor))),
            ),
            const SizedBox(
              height: 10,
            ),
            TextFormField(
              controller: phoneNo,
              validator: (value) {
                if (value == null || value.isEmpty) return 'Field is required.';
                return null;
              },
              decoration: const InputDecoration(
                  label: Text(tPhoneNo),
                  prefixIcon: Icon(Icons.numbers),
                  border: OutlineInputBorder(),
                  labelStyle: TextStyle(color: tSecondaryColor),
                  focusedBorder: OutlineInputBorder(
                      borderSide:
                          BorderSide(width: 2.0, color: tSecondaryColor))),
            ),
            const SizedBox(
              height: 10,
            ),
            TextFormField(
              controller: password,
              validator: (value) {
                if (value == null || value.isEmpty) return 'Field is required.';
                return null;
              },
              obscureText: true,
              decoration: const InputDecoration(
                  label: Text(tPassword),
                  prefixIcon: Icon(Icons.security_rounded),
                  border: OutlineInputBorder(),
                  labelStyle: TextStyle(color: tSecondaryColor),
                  focusedBorder: OutlineInputBorder(
                      borderSide:
                          BorderSide(width: 2.0, color: tSecondaryColor))),
            ),
            const SizedBox(
              height: 10,
            ),
            SizedBox(
              width: double.infinity,
              child: ElevatedButton(
                  onPressed: () async {
                    if (_formKey.currentState!.validate()) {
                      try {
                        final newUser =
                            await _auth.createUserWithEmailAndPassword(
                                email: email.text, password: password.text);
                        if (newUser != null) {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => DashboardScreen(),
                              ));
                        }

                        setState(() {});
                      } catch (e) {
                        print(e);
                      }
                    }
                  },
                  child: const Text(tSignup)),
            ),
          ],
        ),
      ),

2

Answers


  1. Seems you’ve passed the _formKey to the Column() not the Form() widget

        final GlobalKey<FormState>() _formKey = GlobalKey();
        body: Form(
          key: _formKey,
          child: Column()
        ),
    
    Login or Signup to reply.
  2. Make sure to use _formKey on your Form widget

    Form(
      key:_formKey,
    

    and follow this way

      onPressed: () async {
          final isValidate  = _formKey.currentState?.validate()??false; /// here I am providing false on null case.
          if (isValidate) {
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search