flutter dart datetime in list<map> occur error - flutter

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)

Related

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

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.

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: 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;
}

Flutter: the operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'

How can I get the nested object values? print(_user.userLocation()['location']); returns {country: Eesti, city: Tallin}
So, I tried with _user.userLocation()['location']['city'] to get the value Tallinn. However, I am getting
The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'.
print(_user.userLocation()) and print(_user.location().runtimeType); returns
location: {
country: Eesti,
city: Tallinn,
}
flutter: _InternalLinkedHashMap<String, dynamic>
I tried to set a variable to var a = _user.location()['location'] and then a['city']. However, this is not working as well.
You should try like this,
var a = _user.userLocation() as Map;
print(a['location]['city']);
I issue here is dart's type inference is not able to identify the type automatically.So that it gives the error.But the above solution should work