I'm new to flutter dev and trying to convert a model to a json string to pass to an api call.
when i try use the RegistrationRequest.ToJson function I get this error
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
I assume it cannot convert the list of userImages properly and not quite suree how to do it. Any help would be much appreciated
here is where i call the function
var serialisedBodycom = RegistrationRequest.toJson();
var serialisedBody = jsonEncode(serialisedBodycom);
i have a model called RegistrationRequest
import 'UserGender.dart';
import 'UserImage.dart';
class RegistrationRequest {
String email;
String password;
String msisdn;
UserGender gender;
UserGender matchGender;
List<UserImage> userImages;
RegistrationRequest(
{this.email,
this.password,
this.msisdn,
this.gender,
this.matchGender,
this.userImages});
RegistrationRequest.Empty();
RegistrationRequest.fromJson(Map<String, dynamic> json)
: email = json['email'],
password = json['password'],
msisdn = json['msisdn'],
gender = json['gender'],
matchGender = json['matchGender'],
userImages = json['userImages'];
Map<String, dynamic> toJson() {
return {
'email': email ?? '',
'password': password ?? '',
'MSISDN': msisdn ?? '',
'Gender': gender ?? UserGender.Unknown,
'MatchGender': matchGender ?? UserGender.Unknown,
'UserImages': userImages ?? new List<UserImage>()
};
}
}
which contains a list of UserImage
class UserImage {
String base64;
String fileName;
bool isMainProfilePicture;
String contentType;
UserImage(
this.base64, this.fileName, this.isMainProfilePicture, this.contentType);
UserImage.Empty();
UserImage.fromJson(Map<dynamic, dynamic> json)
: base64 = json['base64'],
fileName = json['fileName'],
isMainProfilePicture = json['isMainProfilePicture'],
contentType = json['contentType'];
Map<dynamic, dynamic> toJson() => {
'base64': base64,
'fileName': fileName,
'isMainProfilePicture': isMainProfilePicture,
'contentType': contentType
};
}
the final answer I came up with is the following
RegistrationRequest.fromJson(Map<String, dynamic> json) => RegistrationRequest(
email: json["email"],
password: json["password"],
msisdn: json["msisdn"],
gender: json["gender"],
matchGender: json["matchGender"],
userImages: List<UserImage>.from(json["userImages"].map((x) => UserImage.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"email": email,
"password": password,
"msisdn": msisdn,
"gender": gender,
"matchGender": matchGender,
"userImages": List<dynamic>.from(userImages.map((x) => x.toJson())),