Not sure if this is the best approach, but I am trying to convert a single list into a list of 3 lists based on some filtering to create the view model.
Since a filter (List.where
) may return null, I am trying to implement the null-aware (?) and the if-null (??) operator. However, this is flagged as Conditions must have a static type of 'bool'.
by the IDE.
So basically I am adding 3 List<PropertyTask>
s to a List<List<PropertyTask>>
, where each List<PropertyTask>
is a filter applied on the initial List<PropertyTask>
.
My code looks like this:
factory JobMasterEditViewmodel.fromDomain(JobMaster master)
=> JobMasterEditViewmodel(
tasks: master.tasks == null
? null
: List<List<JobMasterEditTaskViewModel>>
.from([List<JobMasterEditTaskViewModel>
.from(((List<PropertyTask>
.from(master.tasks.where((t)
=> t.taskType == PropertyTaskTypes.always)))? // null-aware
.map((tf) => JobMasterEditTaskViewModel.fromDomain(tf))?) ?? []), // if-null
List<JobMasterEditTaskViewModel> // from here on without null-awareness
.from(List<PropertyTask>
.from(master.tasks.where((t)
=> t.taskType == PropertyTaskTypes.inventory))
.map((tf) => JobMasterEditTaskViewModel.fromDomain(tf))),
List<JobMasterEditTaskViewModel>
.from(List<PropertyTask>
.from(master.tasks.where((t)
=> t.taskType == PropertyTaskTypes.periodically))
.map((tf) => JobMasterEditTaskViewModel.fromDomain(tf))),
List<JobMasterEditTaskViewModel>
.from(List<PropertyTask>
.from(master.tasks.where((t)
=> t.taskType == PropertyTaskTypes.onRequest))
.map((tf) => JobMasterEditTaskViewModel.fromDomain(tf))),
]),
);