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



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

我阅读了下面给出的回复。我需要使用密钥"置信度"从 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>'的原因是您没有等待未来返回,而只是将Future<String>对象取回,请参阅以下内容以获取更多详细信息:

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

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

相关内容

最新更新