我正在尝试将dio集成到我的应用中。我可以通过http包调用api,但是由于dio的高级功能,我正朝dio而不是http
迈进这是我使用http的代码
var body = <String, dynamic>{
doctorAccountEnabledKey: "false",
doctorAccountTypeKey: "doctor",
doctorEmailKey: email,
doctorPasswordKey: password,
doctorPhoneNumberKey: phoneNumber,
doctorGenderKey: gender,
doctorDobKey: dob,
doctorNameKey: {
doctorFirstNameKey: firstName,
doctorLastNameKey: lastName,
doctorMiddleNameKey: middleName,
doctorPrefixKey: prefix,
doctorSuffixKey: suffix
}
};
final response = await http.post(
doctorSignUpUrl,
body: jsonEncode(body),
headers: {"Content-Type": "application/json"},
);
代码工作正常,但是当我使用dio
var body = <String, dynamic>{
doctorAccountEnabledKey: "false",
doctorAccountTypeKey: "doctor",
doctorEmailKey: email,
doctorPasswordKey: password,
doctorPhoneNumberKey: phoneNumber,
doctorGenderKey: gender,
doctorDobKey: dob,
doctorNameKey: {
doctorFirstNameKey: firstName,
doctorLastNameKey: lastName,
doctorMiddleNameKey: middleName,
doctorPrefixKey: prefix,
doctorSuffixKey: suffix
}
};
final response = await Dio().post(
doctorSignUpUrl,
data: jsonEncode(body),
options: Options(contentType: "application/json")
);
以上代码抛出错误,提示LinkedHashMap<String,dynamic> is not subtype of String
我也尝试了以下]
data: json.encode(body),
data: body,
我复制了您的代码,并用双引号将所有键括起来,问题消失了。我将仔细检查并确保所有键都用双引号定义,或者至少在分配值并仔细查看后打印正文。这是我所拥有的,运行正常:
var body = <String, dynamic>{
"doctorAccountEnabledKey": false,
"doctorAccountTypeKey": "doctor",
"doctorEmailKey": "email",
"doctorPasswordKey": "password",
"doctorPhoneNumberKey": "phoneNumber",
"doctorGenderKey": "gender",
"doctorDobKey": "dob",
"doctorNameKey": {
"doctorFirstNameKey": "firstName",
"doctorLastNameKey": "lastName",
"doctorMiddleNameKey": "middleName",
"doctorPrefixKey": "prefix",
"doctorSuffixKey": "suffix"
}
};
final response = await Dio().post(
"https://postman-echo.com/post",
data: jsonEncode(body),
options: Options(contentType: "application/json")
);
复制我拥有的代码,并验证它是否可以在您的计算机上运行,然后慢慢地将其添加回对其他文件的引用,直到它破裂为止,这样做很值得,这样您可以缩小导致错误的原因。
祝你好运!