Reputation: 337
I have a User
object and I want to be able to decode it from JSON and convert it to a JSON as well. fromJSON()
method works fine but when I try to add toJSON()
method the compiler gets mad. Any idea on this?
Missing type arguments for map literal. Try adding an explicit type, or remove implicit-dynamic from your analysis options file.
Here is my User
model
class User {
int id;
String userName;
User({this.id, this.userName});
factory User.fromJson(dynamic json) {
return User(
id: int.parse(json['id'].toString()),
userName: json['user_name'].toString());
}
Map<String, dynamic> toJson() =>
{
'id': id,
'user_name': userName,
};
}
Upvotes: 1
Views: 969
Reputation: 6353
As per error message, you have implicit-dynamic
forbidden in your static analysis settings. You have 2 options:
Check your analysis_options.yml
file, look for implicit-dynamic: false
line and delete it (or change to true
).
Add explicit type to the map in your code:
Map<String, dynamic> toJson() => <String, dynamic>{
'id': id,
'user_name': userName,
};
I would go with the second way because implicit dynamics can lead to some subtle errors, and it's better to forbid them.
Upvotes: 2