类型"_InternalLinkedHashMap<字符串,字符串>"不是类型转换中类型"字符串"的子类型



我试图在Strapi中发布一个注意到我的API,但我有那个错误[ERROR:flutter/lib/ui/ui_dart_state.cc(209)]未处理异常:类型'_InternalLinkedHashMap<String,>'不是类型转换中类型'String'的子类型

这是我的代码

Map<String, String> header = {
"Authorization": "Bearer ${sharedp!.getString('token')}"
};
addPosts() async {
var response = await http.post(
Uri.parse(post_note),
body: {
"data": {
"title": "upload api",
"content": "upload api to the user and show it in the app by id",
"userid": sharedp!.getString('id')
}
},
headers: header,
);
var responsebody = jsonDecode(response.body);

print(responsebody);
return responsebody;
}

那我是怎么在邮差里发邮件的

{
"data":{
"title":"upload api",
"content":"upload api to the user and show it in the app by id",
"userid":1
}
}

您可以将json包装在模型类中,并通过执行以下操作获得您想要的内容,

class ApiRequestData {
/*
{
"title": "upload api",
"content": "upload api to the user and show it in the app by id",
"userid": 1
} 
*/
String? title;
String? content;
int? userid;
Map<String, dynamic> __origJson = {};
ApiRequestData({
this.title,
this.content,
this.userid,
});
ApiRequestData.fromJson(Map<String, dynamic> json) {
__origJson = json;
title = json['title']?.toString();
content = json['content']?.toString();
userid = json['userid']?.toInt();
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['title'] = title;
data['content'] = content;
data['userid'] = userid;
return data;
}
Map<String, dynamic> origJson() => __origJson;
}
class ApiRequest {
/*
{
"data": {
"title": "upload api",
"content": "upload api to the user and show it in the app by id",
"userid": 1
}
} 
*/
ApiRequestData? data;
Map<String, dynamic> __origJson = {};
ApiRequest({
this.data,
});
ApiRequest.fromJson(Map<String, dynamic> json) {
__origJson = json;
data = (json['data'] != null) ? ApiRequestData.fromJson(json['data']) : null;
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
if (data != null) {
data['data'] = this.data!.toJson();
}
return data;
}
Map<String, dynamic> origJson() => __origJson;
}
现在,一旦你像下面这样构造了你的ApiRequest模态对象,
setState(() => _isLoading = true); // If you intend to display a ProgressIndicator in the body while loading..
var id = await sharedp!.getString('id');
var req = ApiRequest(
data: ApiRequestData(
title: "upload api",
content: "upload api to the user and show it in the app by id",
userId: id,
),
);
var response = await http.post(
Uri.parse(post_note),
body: jsonEncode(req.toJson()),
headers: header,
);
var responsebody = jsonDecode(response.body);
setState(() => _isLoading = false); // To now display widgets with the loaded data..

很确定这有帮助…

相关内容

  • 没有找到相关文章

最新更新