【发布时间】:2021-04-23 02:43:14
【问题描述】:
我是 Flutter 开发的新手,正在尝试将模型转换为 json 字符串以传递给 api 调用。
当我尝试使用 RegistrationRequest.ToJson 函数时出现此错误
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
我认为它无法正确转换 userImages 列表,并且不太确定如何去做。任何帮助将不胜感激
这里是我调用函数的地方
var serialisedBodycom = RegistrationRequest.toJson();
var serialisedBody = jsonEncode(serialisedBodycom);
我有一个模型叫 注册申请
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>()
};
}
}
其中包含一个 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
};
}
【问题讨论】:
标签: json flutter dart jsonconvert