Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Book : Flutter Apprentice First Edition, I had below code:

import 'package:flutter/painting.dart';
// 1
enum Importance { low, medium, high }
class GroceryItem {
// 2
final String id;
// 3
final String name;
final Importance importance;
final Color color;
final int quantity;
final DateTime date;
final bool isComplete;
...

And the build method as

Widget buildDate() {
    final dateFormatter = DateFormat('MMMM dd h:mm a');
    final dateString = dateFormatter.format(item!.date);
    return Text(
      dateString,
      style: TextStyle(decoration: textDecoration),
    );
  }

I have the below error:

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

like image 600
abhimanyu Avatar asked Oct 16 '25 11:10

abhimanyu


1 Answers

This removed the error-

final dateString = dateFormatter.format(item!.date as DateTime);
like image 166
abhimanyu Avatar answered Oct 18 '25 06:10

abhimanyu