checkCodeid function
- required String codeid,
This function receives the String codeid and sends it to the
procesarSDMT service in the API to check if it is valid. It returns an integer:
-1 if there was a mistake connecting with the API
1 if the code is valid and has never been used
2 if the code is not valid
3 if the code is valid but has already been used
Implementation
Future<int> checkCodeid ({required String codeid}) async {
final url = Uri.parse('http://apii01.etsii.upm.es/AppCognit/procesarSDMT');
final response = await http.post(
url,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: {
'codeid': codeid,
}
);
if (response.statusCode == 200) {
print('Datos enviados Correctamente: ${response.body}');
final data = jsonDecode(response.body);
if(data['message'] == 'OK'){
if(data['exists'] == 0) {
return 1;
} else{
final List<dynamic> handsUsed = data['hands'];
if(handsUsed.isNotEmpty){
return 3;
} else {
return 1;
}
}
}
else {
return 2;
}
}
print('Error al Enviar datos: ${response.statusCode}');
return -1;
}