i am developing a mobile application with flutter and laravel at the back end, i made an api to add new record, and when i test the api with Thunder client, it worked perfectly, but when i call it from flutter i got null response.
this is how the error looks lik
final _messengerState = scaffoldKey.currentState; // this variable is for the snackbar
scanParticipant() async {
Map data = {
'event': 1,
'participant': 11,
'recorder': 6,
};
print(data);
Response response = await _crud.postRequest(addParticipantRoute, data);
...
}
postRequest(String url, Map data) async {
try {
var response = await http.post(Uri.parse(url),
headers: {"Accept": "application/json"}, body: data);
return response;
} catch (e) {
print('Error catch $e');
}
}
and the backend
public function addRecord(Request $request)
{
$fields = $request->validate([
'event' => 'required|integer',
'participant' => 'required|integer',
'recorder' => 'required|integer'
]);
$user = Record::where('event',$fields['event'])->where('participant',$fields['participant'])->first();
if($user == null)
{
Record::create([
'event' => $fields['event'],
'participant' => $fields['participant'],
'recorder' => $fields['recorder'],
'moment' => Carbon::now()->addHour(),
]);
return response([
'message' => 'success',
],201);
}else {
return response([
'message' => 'already scanned'
],422);
}
}
note : i login before trying this function
I fixed the problem later. The problem was in the data variable, I should turn all the fields into string then send it.
Map data = {
'event': '1',
'participant': '11',
'recorder': '6',
};