Home > Software engineering >  null-aware operator ? not recognized
null-aware operator ? not recognized

Time:12-17

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))),
            ]),
);            

CodePudding user response:

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))),
            ]),
); 

  • Related