List<ServiceModel> serviceList = [
ServiceModel(id: "12", name: "Repair", subServiceList: [
SubServiceModel(name: "Toilet Repair", id: "123"),
SubServiceModel(name: "SafetyTank Repair", id: "456"),
SubServiceModel(name: "Roof Repair", id: "789")
]),
ServiceModel(id: "13", name: "Fixing", subServiceList: [
SubServiceModel(name: "Roof fix", id: "222"),
SubServiceModel(name: "pipe fix", id: "111")
]),
];
I have data like above.. What i need to do is make a multiple selection with respect to servicemodel and also can remove any from the subservicelist..So that i can have only specific selected data with respect to the specific serviemodel..
CodePudding user response:
Firstly, you need to make one intance of ServiceModel is different from others:
class ServiceModel{
final String id;
final String name;
ServiceModel({required this.name, required this.id });
@override
bool operator ==(other) => other.hashCode == hashCode && other is ServiceModel;
@override
int get hashCode => id.hashCode;
}
It will give following:
var service1 = ServiceModel(id:"1", name:"service1");
var service2 = ServiceModel(id:"2", name:"service2");
var service3 = ServiceModel(id:"1", name:"service3")
//false
print(service1 == service2);
//true, because their ids are same
print(service1 == service3);
Then let's make a ServiceModel tree entity.Each serviceModel will have subServices( children ). Why tree? It will give you flexibility that you can make nested ServiceModel of not only 2 but whatever levels you want. Updating the code above:
class ServiceModel {
final String id;
final String name;
final List<ServiceModel> subServices;
ServiceModel(
{required this.name,
required this.id,
this.subServices = const [],});
@override
bool operator ==(other) =>
other.hashCode == hashCode && other is ServiceModel;
@override
int get hashCode => id.hashCode;
}
Now modifying your code build servicesList:
List<ServiceModel> serviceList = [
ServiceModel(id: "12", name: "Repair", subServices: [
ServiceModel(name: "Toilet Repair", id: "123"),
ServiceModel(name: "SafetyTank Repair", id: "456"),
ServiceModel(name: "Roof Repair", id: "789")
]),
ServiceModel(id: "13", name: "Fixing", subServices: [
ServiceModel(name: "Roof fix", id: "222"),
ServiceModel(name: "pipe fix", id: "111")
]),
];
Then how you can build list of selected ServiceModels? Easy:
List<ServiceModel> selectedServices = [];
List<ServiceModel> serviceList = [
ServiceModel(id: "12", name: "Repair", subServices: [
ServiceModel(name: "Toilet Repair", id: "123"),
ServiceModel(name: "SafetyTank Repair", id: "456"),
ServiceModel(name: "Roof Repair", id: "789")
]),
ServiceModel(id: "13", name: "Fixing", subServices: [
ServiceModel(name: "Roof fix", id: "222"),
ServiceModel(name: "pipe fix", id: "111")
]),
];
//you may add whatever service, each of them has unique hashCode according to id
selectedServices.add(serviceList.first.subServices.first);
//true
print(selectedServices
.contains(ServiceModel(name: "Toilet Repair", id: "123")));
CodePudding user response:
Try adding a selected
property to the service model.
You can get the all the selected service model by
List<ServiceModel> serviceModel = serviceList.map((e) => e.selected == true).toList();