В приведенном ниже коде я пытаюсь дождаться возврата значений из функции faceAPIOne () и faceAPITwo, из функции faceVerify (). В настоящее время я получаю метод, вызываемый по нулевому исключению, и позже я получаю значение 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';
}
Спасибо за помощь заранее.