试图弄清未来,异步,在扑扑中等待认知面部识别

问题描述 投票:0回答:1

在下面提到的代码中,我试图等待faceVerify()函数从faceAPIOne()和faceAPITwo函数返回的值。目前,我正在获取在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';
}

感谢您的事先帮助。

asynchronous flutter dart async-await future
1个回答
0
投票

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.