The arguments object must be serializable via the StandardMessageCodec in restorablePush - flutter

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

Related

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)

Firebase Realtime Database does not store Map with $type field

I have a class which I convert to a Map<String, dynamic> which contains a type field:
Map<String, dynamic> _$LibraryFolderToJson(LibraryFolder instance) =>
<String, dynamic>{
r'$type': instance.type,
'id': const GuidConverter().toJson(instance.id),
'parentId': const NullableGuidConverter().toJson(instance.parentId),
'relativePath': instance.relativePath,
};
Now I want to store this Map inside my Firebase Realtime Database, but it will not get stored. So I started to write some tests and found out, that the "$type" field is the problem. When I remove this line, everything is stored as it should.
Are there any restrictions from Firebase which prevents these type of fields?
Is there still a way to store this field?
This type of field comes from the .Net world (Newtonsoft.Json) and I adapted it into my flutter app. Is there a better field name for this?

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

json serializable: variables declared in model class but does not exist in json

I have a problem,
I have a simple model class, that has few variables, but, one of them is not necessarily present all the time.
but JsonSerializable doesn't convert json to model object if that one key is not available in json data.
Sample code -
import "package:json_annotation/json_annotation.dart";
part "address.g.dart";
#JsonSerializable()
class Address {
final String country_code, state, city, street_line1, street_line2, post_code;
Address(this.country_code, this.state, this.city, this.street_line1,
this.street_line2, this.post_code);
factory Address.fromJson(Map<String, dynamic> json) =>
_$AddressFromJson(json);
Map<String, dynamic> toJson() => _$AddressToJson(this);
}
So as you can see, it is a model class, but street_line2 is not always present. and json serializable doesnot work if it's not present.
use "?" for nullable variables in your model
like this: String? then it can be null

Expected a value of type 'int', but got one of type 'String' when trying to add a new key,value to a map

I have a map like this:
map = {
'container_1':0,
'container_2':0,
'container_3':0,
}
That's being created from an iterable like this:
map=Map.fromIterable(containerList, key: (e)=>e, value: (e)=>0);
But when I try to add this key and value, I get an error:
map['user']='User Name';
The error I get is:
Expected a value of type 'int', but got one of type 'String'
How do I add a key value to a map that has a different value type than what's already in it?
The type of the map variable is Map<String, int>, so you couldn't add a String value to it. If you can change map type to Map<String, Object> then you will be able to add String value to it. Like this:
final map = <String, Object>{
'container_1': 0,
'container_2': 0,
'container_3': 0,
};
map['user'] = 'User Name';
I'd prefer to initialize the map to have The key as String and the value as dynamic
Because the dynamic makes your variable type to be determined at the run type with any errors
final map = <String, dynamic>{
'container_1': 0,
'container_2': 0,
'container_3': 0,
};
map['user'] = 'User Name';