I want to do two dependent dropdown buttons of a list of car manufacturers(the first dropdown) and car models/brands(the second dropdown)? But when I click on a brand(the second dropdown) I want to be able to get the category of the brand, so I can print and show if it's a car, truck, bus, and so on. Been having problems implementing that.
An example of the JSON file
List _cars = [
{
"make": "Acura",
"brand": [
{"name": "CL", "category": "Coupe"},
{"name": "ILX", "category": "Sedan"},
{"name": "Integra", "category": "Sedan"},
{"name": "Legend", "category": "Sedan"},
{"name": "MDX", "category": "SUV"},
{"name": "NSX", "category": "Coupe"},
{"name": "RDX", "category": "SUV"},
{"name": "RL", "category": "Sedan"},
{"name": "RLX", "category": "Sedan"},
{"name": "RSX", "category": "Coupe"},
{"name": "SLX", "category": "SUV"},
{"name": "TL", "category": "Sedan"},
{"name": "TLX", "category": "Sedan"},
{"name": "TSX", "category": "Sedan"},
{"name": "Vigor", "category": "Sedan"},
{"name": "ZDX", "category": "SUV"},
]
},
{
"make": "Alfa Romeo",
"brand": [
{"name": "147", "category": "Sedan"},
{"name": "164", "category": "Sedan"},
{"name": "4C", "category": "Coupe"},
{"name": "4C Spider", "category": "Convertible"},
{"name": "Brera", "category": "Sedan"},
{"name": "Giulia", "category": "Sedan"},
{"name": "Giulietta", "category": "Sedan"},
{"name": "GT", "category": "Sedan"},
{"name": "Mito", "category": "Sedan"},
{"name": "Stelvio", "category": "SUV"},
]
}
];
The Model for the JSON file
class CarsModel {
late String make;
late List<Brand> brand;
CarsModel({required this.make, required this.brand});
CarsModel.fromJSON(Map<String, dynamic> json) {
make = json['make'];
brand = json['brand'].map<Brand>((json) => Brand.fromJSON(json)).toList();
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data['make'] = this.make;
data['brand'] = this.brand;
return data;
}
}
class Brand {
late String name;
late String category;
Brand({required this.name, required this.category});
Brand.fromJSON(Map<String, dynamic> json) {
name = json['name'];
category = json['category'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data['name'] = this.name;
data['category'] = this.category;
return data;
}
}
The Implementation
getBrands(String make) async {
await Future.delayed(Duration(seconds: 4), () {
print('Future.delayed');
});
return Future.value(_cars
.map((map) => CarsModel.fromJSON(map))
.where((item) => item.make == make)
.map((e) => e.brand.map((e) => e.name))
.expand((i) => i)
.toList());
}
//My problem is how to get the category based on the brand name selected
getCategory(String make) async {
await Future.delayed(Duration(seconds: 4), () {
print('Future.delayed');
});
return Future.value(_cars
.map((map) => Brand.fromJSON(map))
.where((item) => item.name == make)
.map((e) => e.category)
.first
.toString());
}
List<String> getMakes() => _cars
.map((map) => CarsModel.fromJSON(map))
.map((item) => item.make)
.toList();
Calling get category
category = await getCategory(value);
This is the error i get
Unhandled Exception: type 'Null' is not a subtype of type 'String'