'Unhandled Exception: type 'Null' is not a subtype of type 'DateTime' in type cast

ElevatedButton(
            onPressed: () async {
              bool validated = formKey.currentState!.validate();
              if (validated) {
                formKey.currentState!.save();
                final Map<String, dynamic> data =
                    Map<String, dynamic>.from(formKey.currentState!.value);
                data['date'] =
                    (data['date'] as DateTime).millisecondsSinceEpoch;
                
                await eventDBS.create(data);
                Navigator.pop(context);
              }

When i click on the button occur the error "Unhandled Exception: type 'Null' is not a subtype of type 'DateTime' in type cast" and the currentState shows error without the null check(!), i don't know what to do, i really appreciate any help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source