Home > Enterprise >  The argument type 'Object' can't be assigned to the parameter type 'String'
The argument type 'Object' can't be assigned to the parameter type 'String'

Time:10-29

Here is the part of the widget the i´m having the problem

DateTime? _selectedDate;

(...)

     Container(
        height: 70,
            child: Row(children: [
              Text(_selectedDate == null
                  ? 'No date chosen!'
                  : DateFormat.yMd(_selectedDate)),
              TextButton(
                child: Text(
                  'Choose date',
                  style: TextStyle(fontWeight: FontWeight.bold),
                ),
                onPressed: _presentDatePicker,
              )
            ]),
          ),
(...)

The problem happens when i check if _selectedDate is null, insed the Text widget, just like i´m showing on the image

enter image description here

CodePudding user response:

The usage of DateFormat is a bit different. The first, optional argument is the locale for the DateFormat. DateFormat has a method format that takes DateTime as a argument and outputs a formatted String.

DateFormat.yMd().format(_selectedDate!)

If you are using null-safety then you will also need to add ! at the end of the _selectedDate. Dart unfortunetely doesn't promote (from nullable to non-nullable) variables that are properties.

  • Related