_CastError (type 'String' is not a subtype of type 'DateTime' in type cast) - flutter

I am trying to add a Date (yyyy/mm/dd) to Firebase and when converting the _selectedDate to a yMd format I am getting this error somnewhere else in my code _CastError (type 'String' is not a subtype of type 'DateTime' in type cast)
Declare variable:
String _selectedDate = DateFormat("yyyy/mm/dd").format(DateTime.now()).toString();
The place where you can enter the date:
MyInputField(
title: "Date",
hint: DateFormat.yMd().format(_selectedDate))

I guess hint requires String and already you converted DateTime.now() to String. So, you may not be allowed to call format for _selectedDate.
MyInputField(
title: "Date",
hint: _selectedDate)
or in assignment
DateTime _selectedTime = DateTime.now();
You are supposed to user either a way, not both.

Related

The parameter 'abc' can't have a value of 'null' because of its type, but the implicit default value is 'null'

#required to a property gave such ERROR:
The parameter 'lng' can't have a value of 'null' because of its type, but the implicit default value is 'null'. (Documentation)
but removing #, removes the error. how ?
I mean, the value still can be null. What does it have to do with "#" symbol.
see pictures:
You dont need to use # anymore, just use required.
({
required this.lat,
required this.lng,
})
if you like to accept null data, use DataType? varName.
String? lat;
String? lng;
More about understanding-null-safety

Flutter type 'String' is not a subtype of type 'DateTime' in type cast [duplicate]

This question already has answers here:
Flutter: Invalid date format 24-09-2022
(3 answers)
Closed 5 months ago.
I am trying to compare date and asper date i am doing some actioin, but I am getting following error.
"type 'String' is not a subtype of type 'DateTime' in type cast"
I am getting date from API in this format 25-09-2022
var postList have all data from api(in this case i am getting start and end date from API)
following are my code:-
DateTime stdt = DateFormat('dd-MM-yyyy').parse(postList.startDate.toString());
DateTime endt = DateFormat('dd-MM-yyyy').parse(postList.endDate.toString());
DateTime crdt = DateFormat('dd-MM-yyyy').format(DateTime.now()) as DateTime;
if (stdt.isAtSameMomentAs(endt)){
if(stdt.isBefore(crdt)){
setState(() {
ComparisonText = "Past";
ContainerColor = Colors.red;
});
}
enter image description here
you are trying to compare string to date time in this line
if(stdt.isBefore(crdt))
try add .toString() like that
DateTime crdt = DateFormat('dd-MM-yyyy').format(DateTime.now().toSring());

flutter dart datetime in list<map> occur error

my code is below. I want it to be type-recognized as a datetime type at compile time.
var myList = [{
'message': 'foo',
'time': DateTime.now()
}];
DateTime.now().difference(myList[0]['time']);
and it has error of The argument type 'Object?' can't be assigned to the parameter type 'DateTime'.
how can i fix this?
You need to add a type cast for this with the as keyword:
DateTime.now().difference(myList[0]['time'] as DateTime)

type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Client' in type cast in flutter?

I want to get the Client name from the Api using fromMap() method as shown bellow:
factory Order.fromMap(Map<String, dynamic> map) {
return Order(
created_at: Tracker.decode(map['created_at']),
id: map['id'],
updated_at: Tracker.decode(map['updated_at']),
total_price: map['total_price'],
status: map['status'],
client: map['client']
);
}
client is an object of Client Model ..
I got the following error:
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Client' in type cast...
thank you for your help!
You are trying to assign a data of type Map<String, dynamic> to the client which seems to the of type Client.
You need to convert the map['client'] into the Client by using Client.fromMap(map['client']) assuming you have the Client model

Flutter: type 'DateTime' is not a subtype of type 'String'

Text(
DateTime.parse(documents[index]['createdAt']
.toDate()
.toString()) ??
'default',
)
I am trying to get date from firestore. I have DateTime.now() stored in createdIn in firestore.
because the format of both is not the same.
step 1: you can create DateFormat.
step 2: use 'DateFormat'.parse().
try reading this: https://help.talend.com/r/6K8Ti_j8LkR03kjthAW6fg/atMe2rRCZqDW_Xjxy2Wbqg
Here, it is expecting type as 'String', but we are passing it as a 'DateTime' which is incorrect.
Formatting dates in the default 'en_US' format does not require any initialization.
https://api.flutter.dev/flutter/intl/DateFormat-class.html
So, we simply need to format it like:
var dateTime = DateTime.now()
DateFormat.E().format(dateTime)
E() is the constructor of DateFormat in Flutter which refers to first 3 letter of the week
You can refer the official doc for available constructors:
https://api.flutter.dev/flutter/intl/DateFormat-class.html#constructors
Note:
Below is the version used for DateTime library ~ intl
intl: ^0.18.0
(https://pub.dev/packages/intl)
If it's a Timestamp field, cast it to Timestamp. If you need that as a DateTime, use a .toDate on that. Don't process it through a string... completely unnecessary.
It might help you
String? changeDateFormatWithInput(String? currentDate, currentFormat,
requiredFormat) {
String? date;
try {
if (currentDate != null && currentDate.isNotEmpty) {
DateTime tempDate =
new DateFormat(currentFormat).parse(currentDate, true);
date = DateFormat(requiredFormat).format(tempDate.toLocal());
return date;
}
} catch (e) {
print(e);
return null;
}
return date;
}