1

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))),
            ]),
);            
w461
  • 2,168
  • 4
  • 14
  • 40
  • Is the if-null flagged? Which specific section of code gets flagged by the IDE? – TarHalda Dec 16 '22 at 14:40
  • 1
    `(List.from(master.tasks.where((t) => t.taskType == PropertyTaskTypes.always)))` has the error text. The `)` in `?) ?? []` flags `Expected to find ':'.`, which results from mistakenly understanding `?` as a condition and not as null-aware – w461 Dec 16 '22 at 14:47

1 Answers1

1

That list will never be null, so trying to make the code null-aware there results in the compiler misinterpreting it to mean the ternary operator. If you remove the null-awareness code, the error is removed as well.

If code cannot be null for any values inputted (which this cannot; List documentation shows that it cannot return null), then trying to add null-aware code to it will result in the compiler being confused.


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))) 
                  .map((tf) => JobMasterEditTaskViewModel.fromDomain(tf)))), 
            List<JobMasterEditTaskViewModel> 
              .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))),
            ]),
); 

TarHalda
  • 1,050
  • 1
  • 9
  • 27
  • 1
    Thanks, you are absolutely right. Somehow I assumed `.where()` would return `null` if it doesn't find anything. But I just checked on DartPad and it is `[ ]`. Maybe I should have questioned this assumption a bit earlier... – w461 Dec 16 '22 at 15:05