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
Seems you’ve passed the _formKey to the Column() not the Form() widget
Make sure to use
_formKey
on yourForm
widgetand follow this way