如何从 Future<String> 形式的 json 对象中获取值?

How to get the values from json object which is in the form Future<String>?

我正在使用 aws_ai 插件,响应的形式是 instance of Future<String>

我阅读了下面给出的回复。我需要使用密钥 "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<String>'

感谢您的帮助!

您得到 Instance of 'Future<String>' 结果的原因是您没有等待 return 的未来,而只是将 Future<String> 对象取回 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";
        }
}