【问题标题】:How to get the values from json object which is in the form Future<String>?如何从 Future<String> 形式的 json 对象中获取值?
【发布时间】:2019-07-12 19:18:29
【问题描述】:

我正在使用 aws_ai 插件,响应的形式为 instance of Future&lt;String&gt;

我阅读了下面给出的回复。我需要使用键“confidence”从 json 访问特定值,如何访问它?


Future main1() async {

  File sourceImagefile; //load source image in this File object
  String  accessKey = "",
          secretKey = "",
          region    = "" ;

  RekognitionHandler rekognition = new RekognitionHandler(accessKey, secretKey, region);
  if(sourceImagefile !=null && targetImagefile !=null) {
      Future<String> labelsArray = rekognition.compareFaces(
          sourceImagefile, targetImagefile);
      print(labelsArray);
      return labelsArray.toString();
    }else{
    return "Enter Image";}

}
___________________________________
(later in widget build:)
___________________________________
onpressed(){
main1().then((labelsArray){
    print("json value is:  "+labelsArray);
  });
}

目前的结果是:

json value is: Instance of 'Future&lt;String&gt;'

感谢您的帮助!

【问题讨论】:

  • 能否提供更详细的代码。
  • 确定我编辑了问题
  • 看看应该能解决的答案

标签: json string flutter dart future


【解决方案1】:

您获得Instance of 'Future&lt;String&gt;' 的原因是您没有等待未来返回,而只是将Future&lt;String&gt; 对象返回refer this 以获取更多详细信息:

下面的代码应该可以解决你的问题:

Future<String> futureFunction() async {
    RekognitionHandler rekognition = new RekognitionHandler(accessKey, secretKey, region);
        if(sourceImagefile !=null && targetImagefile !=null) {
          var labelsArray = await rekognition.compareFaces(
              sourceImagefile, targetImagefile);
          print(labelsArray);

          return labelsArray.toString();
        } else {
            return "enter image";
        }
}

【讨论】:

  • 感谢您的回复。此用于检索响应的代码是否正确? ```futureFunction().then((val){ print("json is: "+val); });
  • 是的,这可能是它识别图像所需的时间
  • 感谢您的回复。此用于检索响应的代码是否正确? ```futureFunction().then((val){ print("json is: "+val); });
  • 是的,你可以使用它
猜你喜欢
  • 2022-06-10
  • 1970-01-01
  • 2019-07-01
  • 2020-04-04
  • 2019-06-29
  • 1970-01-01
  • 2020-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多