type'_internallinkedHashmap< string,string>'不是类型的子类型' string'在类型中

发布于 2025-02-02 21:54:39 字数 918 浏览 5 评论 0原文

我试着在strapi中向我的API发布一张笔记,但我有错误 [错误: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
    }
}

I am try to post an note to my API in Strapi but I have that error
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type '_InternalLinkedHashMap<String, String>' is not a subtype of type 'String' in type cast

that is my code

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;
    }

then that how I make post in postman

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我偏爱纯白色 2025-02-09 21:54:39

您可以将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..

请确保这会有所帮助。

You can just wrap the json in a model class and obtain what you want by doing the following,

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;
}

Now once you construct your ApiRequest modal object like following,

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..

Pretty sure this helps..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文