Here, I take two dropdowenbuttonformfield, first for category and second for subcategory. The data of subcategory is depending on the category selected in dropdowenbuttonformfield. The error is occurred when the value of both is selected and after that if i change the category it showed error in subcategory dropdownbuttonformfield.. From my understanding it should show value of subcategory 'None'. So here what is the problem. Here is my code.
class _EditProductScreenState extends State<EditProductScreen> {
final _priecFocus = FocusNode();
final _descriptionFocus = FocusNode();
final _imageFocus = FocusNode();
var _catProvider;
late List<String> category;
late List<String> subCategory;
var _selectedCat;
var _selctedSubCat;
bool isLoading = true;
@override
void didChangeDependencies() {
if (isLoading) {
_catProvider = Provider.of<Category>(context, listen: false);
category = _catProvider.categories;
category.insert(0, 'None');
isLoading = false;
}
_selectedCat = category[0];
super.didChangeDependencies();
}
@override
void dispose() {
_priecFocus.dispose();
_descriptionFocus.dispose();
_imageFocus.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final d = _catProvider.subCategories(_selectedCat);
subCategory = d == [] ? subCategory : d;
subCategory.insert(0, 'None');
for (var i in subCategory) {
print(i);
}
return Scaffold(
appBar: AppBar(
title: const Text('Edit Product'),
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 10),
child: Form(
child: ListView(
children: [
///... for category and subcategory
Row(
children: [
Expanded(
flex: 2,
child: Text('Category : ',
style: TextStyle(
fontSize: 16,
color: Color.fromARGB(255, 123, 123, 123))),
),
Expanded(
flex: 4,
child: DropdownButtonFormField(
value: category[0],
onChanged: (val) {
setState(() {
_selectedCat = val;
});
},
items: category
.map<DropdownMenuItem<Object>>(
(val) => DropdownMenuItem(
value: val,
child: Text(val),
),
)
.toList(),
),
),
],
),
//... subcategory
Row(
children: [
Expanded(
flex: 2,
child: Text('SubCategory : ',
style: TextStyle(
fontSize: 16,
color: Color.fromARGB(255, 123, 123, 123))),
),
Expanded(
flex: 4,
child: DropdownButtonFormField(
value: subCategory[0],
onChanged: (val) {
setState(() {
_selctedSubCat = val;
});
},
items: subCategory
.map<DropdownMenuItem<Object>>(
(val) => DropdownMenuItem(
value: val,
child: Text(val),
))
.toList(),
),
),
],
)
],
),
),
),
);
}
}