type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Client' in type cast in flutter? - 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

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 arguments object must be serializable via the StandardMessageCodec in restorablePush

I am trying to pass a map to the arguments parameter in the restorablePush method in flutter. Nonetheless, I keep getting an erro stating that the object I pass as a parameter should be serialized.
Error:
'debugIsSerializableForRestoration(arguments)': The arguments object must be serializable via the StandardMessageCodec.
This is my code
Navigator.restorablePush(context, _buildRoute,
arguments: <String, dynamic>{
'latitude': latitudeVisit,
'longitude': longitudeVisit,
'uid': widget.user.userUID,
'user': widget.user,
});
I thought a map was a serializable object

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)

Convert data Type into different data Type in dart

I am working in backend using dart. when i get a request i check the data type of all the values in key value pair. some times i receive data type.
_InternalLinkedHashMap<String, dynamic>
I need a where i can type cast a data-type
Type xyz = sumFunction(_InternalLinkedHashMap<String, dynamic>);
print(xyz);
output :
Map<String,dynamic>
Beacause when i am comparing
print(_InternalLinkedHashMap<String, dynamic> == Map<String,dynamic>);
output:
false
If anyone have any kind of solution please provide.

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