Reputation: 37
How can I check if there is a selected ìtem in my DropdownButton
before I press a Button
and Toast
a error message if no item is selected?
I have been searching for a solution but no hope yet.
I am checking if all my TextField
is empty, and that works. But haven't found any solution for the DropdownButton
. The ElevatedButton
saves the data to firebase.
Here is a snippet of my code:
child: DropdownButtonFormField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey)
),
),
items: _isAdmin
.map((value) => DropdownMenuItem(
child: Text(
value,
style: TextStyle(color: Colors.black),
),
value: value,
))
.toList(),
onChanged: (selectedadmin){
setState(() {
isadmin = selectedadmin;
});
},
value: isadmin,
isExpanded: false,
elevation: 10,
),
),
ElevatedButton(
onPressed: () async {
var Name = nameController.text.trim();
var email = emailController.text.trim();
var password = passwordController.text.trim();
var mobilenumber = mobileController.text.trim();
var empid = employeidController.text.trim();
if (Name.isEmpty ||
email.isEmpty ||
password.isEmpty ||
empid.isEmpty ||
mobilenumber.isEmpty) {
// show error toast
Upvotes: 0
Views: 824
Reputation: 143
Unlike TextFormField, DropdownButtonFormField doesn't have a controller. However you have the onChange
method, where you are setting the isadmin variable. You should check the value of the isadmin
var within the onPressed
in your button.
Upvotes: 1