I am new on flutter and I try to create a dependent dropdown. I have 2 dropdowns fields and my data are the categories and the products. The first dropdown which display the categories is working. My problem is on the second dropdown, are not showing the options based on the selected value of categories. Below I add my data in a specific file.
localData.dart
import 'package:flutter/material.dart';
class LocalData with ChangeNotifier {
static const List<Map<String, dynamic>> _categories = [
{'id': 1, 'name': 'Electronics'},
{'id': 2, 'name': 'Gaming'},
{'id': 3, 'name': 'Accessories'},
];
static List<Map<String, dynamic>> get categories {
return [..._categories];
}
static List<Map<String, dynamic>> _products = [
{
'id': 1,
'name': 'Smartphone',
'categoriesId': [1]
},
{
'id': 2,
'name': 'Laptop',
'categoriesId': [2, 3]
},
{
'id': 3,
'name': 'iPods',
'categoriesId': [1, 3]
},
];
static List<Map<String, dynamic>> get products{
return [..._products];
}
Basically on the products data(_products) I have categoriesId which the list of ids are combine with the ids of categories(_categories). In my code I use the "where" method but is not working. Also I used the contains but I had the same result. This is my code:
home.dart
class _HomePageState extends ConsumerState<HomePage> {
int? category;
int? product;
List<Map<String, dynamic>> loadProd = [];
@override
Widget build(BuildContext context) {
final themeService = ref.watch(themeServiceProvider);
final ancScaffold = Scaffold.maybeOf(context);
getProductsOptions() {
loadProd = LocalData.products
.where(
(prod) => prod['categoriesId'] == category,
)
.toList();
if (LocalData.products.isNotEmpty) {
return loadProd.map((val) {
return DropdownMenuItem(
value: val['id'], child: Container(child: Text(val['name'])));
}).toList();
}
}
return Scaffold(
appBar: AppBar(
bottom: themeService.appbarBottom(),
elevation: 0,
),
body: SingleChildScrollView(
child: Column(
children: [
Material(
child: Container(
child: Form(
child: Column(
children: [
SizedBox(
child: DropdownButtonFormField(
value: category,
isExpanded: true,
decoration: InputDecoration(
labelText: 'Categories',
),
items: LocalData.categories.map((cat) {
return DropdownMenuItem(
value: cat['id'],
child: Container(child: Text(cat['name'])));
}).toList(),
onChanged: (value) {
setState(() {
if (value == null) {
return;
}
category = value as int;
//print(categories);
});
},
),
),
SizedBox(
child: DropdownButtonFormField(
value: product,
isExpanded: true,
decoration: InputDecoration(
labelText: 'Products',
),
//value: categories,
items: getProductsOptions(),
onChanged: (prod) {
setState(() {
if (prod == null) {
return;
}
product = prod as int;
});
},
),
),
],
),
),
),
),
],
),
),
);
}
}
Any ideas?