Gavin
Gavin

Reputation: 11

I got this error: The argument type 'DateTime?' can't be assigned to the parameter type 'DateTime'

I get this error on the date row:

The argument type 'DateTime?' can't be assigned to the parameter type 'DateTime'.

This is the code:

                          icon: task.taskImage,
                          title: task.tasktitle,
                          time: task.startTime,
                          desc: task.taskDesc,
                          ifDate: true,
                          date: task.taskDate!=null? DateFormat.yMMMd().format(task.taskDate): DateFormat.yMMMd()  as String,
                        ),

I couldn't figure out how to correct. Can you tell me hot to correct the code, please?

Upvotes: 0

Views: 1126

Answers (1)

Ivo
Ivo

Reputation: 23178

To turn a nullable type into a non-nullable you can add ! to it, although it throws an error if it happens to be null.

In this case format() requires a non-nullable type. Since you made sure task.taskDate is not null you can safely apply that operator. So do

DateFormat.yMMMd().format(task.taskDate!)

Upvotes: 6

Related Questions