首页 > 解决方案 > 试图找出未来,异步,等待认知面部识别

问题描述

在下面提到的代码中,我试图等待 faceAPIOne() 和 faceAPITwo 函数的返回值,来自 faceVerify() 函数。目前我正在获取在 null 异常上调用的方法,后来我打印了 faceAPIOne 的值。我究竟做错了什么 ?我试图参考下面提到的链接。https://dart.dev/codelabs/async-await,https://www.youtube.com/watch?v=SmTCmDMi4BY,https://medium.com/flutter-community/futures-async-await-threading- in-flutter-baeeab1c1fe3

您可以使用下面提到的代码来重现问题

FaceOneModel faceOneModel;
var faceVerifyIdOne;
List<dynamic> decodedFace1;
Future<String> faceAPIOne() async {
  final bytes = image.readAsBytesSync();
  var uri = Uri.parse(
      "https://eastus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true");
  var request = http.Request("POST", uri)
    ..headers['Ocp-Apim-Subscription-Key'] =
        "Your Key"
    ..headers['Content-Type'] = "application/octet-stream"
    ..bodyBytes = bytes;

  var response = await request.send();
  print(request);
  print(response.statusCode);
  response.stream.transform(utf8.decoder).listen((value) {
    print(value);
    decodedFace1 = json.decode(value);
    print(decodedFace1);
    faceOneModel = FaceOneModel.fromJson(decodedFace1[0]);
    print("face one function "+faceOneModel.face1);
    // setState(() {
    //   faceVerifyIdOne = faceOneModel.face1;
    // });
  });
  return faceOneModel.face1;
}

var faceTwo;
var _result;
String ensembleUrl = 'my web url';
Future<Uint8List> _networkImageToByte() async {
  final http.Response response = await http.post(
    "My webservice Hyperlink",
    headers: {
      "Content-Type": "application/json",
    },
    body: jsonEncode(
      {
        "custId": phoneUserId,
      },
    ),
  );
  if (response.statusCode == 200) {
    var _jsonConvertor = json.decode(response.body);
    var _jsonConvertorD = _jsonConvertor['d'];
    print(_jsonConvertorD);
    List<dynamic> list = json.decode(_jsonConvertorD);
    NetworkModel networkModel = NetworkModel.fromJson(list[0]);
    _result = networkModel.imageName;
  }
  Uint8List byteImage = await networkImageToByte(ensembleUrl + _result);

  // setState(() {
  //   faceTwo = byteImage;
  // });
  return byteImage;
}

var faceVerifyIdTwo;
FaceTwoModel faceTwoModel;
List<dynamic> decodedFace2;
Future<String> faceAPITwo() async {
  // faceTwo = await _networkImageToByte();
  // final bytes = image.readAsBytesSync();
  var uri = Uri.parse(
      "https://eastus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true");
  var request = new http.Request("POST", uri)
    ..headers['Ocp-Apim-Subscription-Key'] =
        "your Key"
    ..headers['Content-Type'] = "application/octet-stream"
    ..bodyBytes = await _networkImageToByte();

  var response = await request.send();
  print(request);
  print(response.statusCode);
  response.stream.transform(utf8.decoder).listen((value) {
    print(value);
    decodedFace2 = json.decode(value);
    print(decodedFace2);
    faceTwoModel = FaceTwoModel.fromJson(decodedFace2[0]);
    print('face two function'+faceTwoModel.face2);
    // setState(() {
    //   faceVerifyIdTwo = faceTwoModel.face2;
    // });
  });
  return faceTwoModel.face2;
}

ResultModel resultModel;
var verifyFace;
var faceSingle;
var faceDouble;
Future<String> faceVerify() async {
  // print('faceOne call back '+await faceAPIOne());
  // print('facetow call back '+await faceAPITwo());
  // var a = await faceAPIOne();
  // var b = await faceAPITwo();
  // print(a);
  print('face one '+await faceAPIOne());
  final response = await http.post(
    "https://eastus.api.cognitive.microsoft.com/face/v1.0/verify",
    headers: {
      "Ocp-Apim-Subscription-Key": "Your Key",
      "Content-Type": "application/json",
    },
    body: jsonEncode(
      {"faceId1": 'abc', "faceId2": await faceAPITwo()},
    ),
  );
  print(response.body);
  verifyFace = json.decode(response.body);
  print('${verifyFace['isIdentical']}');

  if (verifyFace['isIdentical'] == true) {
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(
            "Success",
            style: TextStyle(color: Colors.green),
          ),
          content: Text(
            "Face Verification Success!!!",
            style: TextStyle(),
          ),
          actions: <Widget>[
            FlatButton(
              child: Text(
                "OK",
                style: TextStyle(),
              ),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => IndexPage()),
                );
                // Navigator.pop(context);
              },
            )
          ],
        );
      },
    );
  } else if (verifyFace['isIdentical'] == false) {
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(
            "Alert",
            style: TextStyle(color: Colors.red),
          ),
          content: Text(
            "Face Verification Failed!!!",
            style: TextStyle(),
          ),
          actions: <Widget>[
            FlatButton(
              child: Text(
                "OK",
                style: TextStyle(),
              ),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => OtpPage()),
                );
              },
            ),
          ],
        );
      },
    );
  }
  return 'success';
}

我在这里先向您的帮助表示感谢。

标签: asynchronousflutterdartasync-awaitfuture

解决方案


faceOneModel到达最后一行时不会填充,faceOneAPI因为流没有机会处理响应。

假设您正在使用package:http,您可以执行以下操作来避免使用流:

Future<String> faceAPIOne() async {
  // ...

  // Takes a StreamResponse and converts it to a Response 
  // which has the entire response body
  final value = await http.Response.fromStream(response);
  faceOneModel = json.decode(value.body);

  // ...

  return faceOneModel.face1;
}

推荐阅读