简体   繁体   English

在 http 调用中将 object 转换为可编码的 object 失败

[英]Converting object to an encodable object failed in http call

I'm getting a response from API but there is an error.我收到了 API 的回复,但出现了错误。 I'm not getting any response where I'm calling topicpurchased()我在调用 topicpurchased() 时没有得到任何回应

Future<bool> topicpurchased(int topicid, String title) async {
  var map = new Map<String, dynamic>();
  map['topicid'] = topicid;
  map['title'] = title;
  var data = json.encode(map);
  print(data);
  var response = await http.post(Constants.ApiBaseUrl + '/topicpuchased',
      headers: headers, body: data);
  if (response.statusCode == 200) {
    print("topicpurchasede" + response.body);
    TrueOrFalse trueOrFalse = TrueOrFalse.fromJson(json.decode(response.body));
    if (trueOrFalse.status == "sucess") {
      print(" 👍👍👍👍👍👍👍👍");
      return true;
    } else {
      print("something went wrong" + trueOrFalse.status);
      return false;
    }
  } else {
    throw Exception('Failed');
  }
}

error in console as follows控制台报错如下

I/flutter (22130): topicpurchasede{"status":"sucess"}
I/flutter (22130): datamodel sucess
I/flutter (22130):  👍👍👍👍👍👍👍👍
E/flutter (22130): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Converting object to an encodable object failed: Instance of 'Future<String>'
E/flutter (22130): #0      _JsonStringifier.writeObject (dart:convert/json.dart:688:7)
E/flutter (22130): #1      _JsonStringifier.writeMap (dart:convert/json.dart:769:7)
E/flutter (22130): #2      _JsonStringifier.writeJsonValue (dart:convert/json.dart:724:21)
E/flutter (22130): #3      _JsonStringifier.writeObject (dart:convert/json.dart:679:9)
E/flutter (22130): #4      _JsonStringStringifier.printOn (dart:convert/json.dart:877:17)
E/flutter (22130): #5      _JsonStringStringifier.stringify (dart:convert/json.dart:862:5)
E/flutter (22130): #6      JsonEncoder.convert (dart:convert/json.dart:262:30)
E/flutter (22130): #7      JsonCodec.encode (dart:convert/json.dart:172:45)

thanks in advance.提前致谢。

First, you have to call the function using await because it's a Future:首先,您必须使用 await 调用 function,因为它是 Future:

Example:例子:

bool response = await topicpurchased(topicid,title);

 Future<bool> topicpurchased(int topicid, String title) async { var map = new Map<String, dynamic>(); map['topicid'] = topicid; map['title'] = title; var data = json.encode(map); print(data); var response = await http.post(Constants.ApiBaseUrl + '/topicpuchased', headers: headers, body: data); if (response.statusCode == 200) { print("topicpurchasede" + response.body); //here could be the bug TrueOrFalse trueOrFalse = TrueOrFalse.fromJson(json.decode(response.body)); if (trueOrFalse.status == "sucess") { print(" "); return true; } else { print("something went wrong" + trueOrFalse.status); return false; } } else { //second: here too you can send a bool return because you function has declare a //bool response throw Exception('Failed'); } }

Third: The error seems to come from your model, you can comment on this line and run the application to see if the error comes from there第三:错误似乎来自您的model,您可以在此行注释并运行应用程序以查看错误是否来自那里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在api调用上将对象转换为可编码对象失败:_LinkedHashMap len:2 - Converting object to an encodable object failed on api call: _LinkedHashMap len:2 由于 404,将 object 转换为可编码 object 失败 - Converting object to an encodable object failed due to 404 在 Flutter 中将对象转换为可编码对象失败 - Converting object to an encodable object failed in Flutter 错误:将 object 转换为可编码的 object 失败 - Error: Converting object to an encodable object failed 将 object 转换为可编码的 object 失败:“偏移”实例 - Converting object to an encodable object failed: Instance of 'Offset' 将 object 转换为可编码的 object 失败:“LeadSource”实例 - Converting object to an encodable object failed: Instance of 'LeadSource' 将对象转换为可编码对象失败:“未来”实例<dynamic> &#39; - Converting object to an encodable object failed: Instance of 'Future<dynamic>' 将对象转换为可编码对象失败:&#39;_CompactLinkedHashSet 的实例<List<String> &gt;&#39; - Converting object to an encodable object failed: Instance of '_CompactLinkedHashSet<List<String>>' Flutter:未处理的异常:将对象转换为可编码对象失败:“AddProjectModel”的实例 - Flutter: Unhandled Exception: Converting object to an encodable object failed: Instance of 'AddProjectModel' 未处理的异常:将 object 转换为可编码的 object 失败:“测量”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'Mesure'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM