I have the following API in a Django project where I am trying to send a log with some variables that are Int and String.
Here is the Api/views.py:
@api_view(['POST', 'GET'])
@permission_classes([IsAuthenticated])
def addlog(request,username, id):
workout = Workout.objects.get(id=id, user=request.user)
if request.method == 'POST':
if workout.active:
active_session = ActiveSession.objects.get(id=ActiveSession.objects.last().id)
form = LogForm(request.POST)
if form.is_valid():
data = Log()
data.user = request.user
data.log_workout = request.POST.get('log_workout')
data.log_exercise = request.POST.get('log_exercise')
data.log_order = request.POST.get('log_order')
data.log_repetitions = form.cleaned_data['log_repetitions']
data.log_weight = form.cleaned_data['log_weight']
data.workout_id = Workout.pk
data.save()
In my flutter app I have created the api_service.dart as following:
static Future<http.Response> addLog(int logWeight, int logRepetitions,
int logOrder, String logExercise, String logWorkout, int id) async {
var url = Uri.parse(Config.apiURL +
Config.userAddlogAPI.replaceFirst("{id}", id.toString()));
try {
final response = await http.post(url, headers: {
HttpHeaders.authorizationHeader:
'Token xxxxxxxxxxxx',
}, body: {
'log_weight': logWeight,
'log_repetitions': logRepetitions,
'log_order': logOrder,
'log_exercise': logExercise,
'log_workout': logWorkout,
});
#.......if to catch error but it doesn't reach this part........................
}
Here the screen.dart
Form(
child: Expanded(
child: Column(
children: [
TextFormField(keyboardType:TextInputType.number,
onChanged:(value) {final int?parsedValue = int.tryParse(value);
if (parsedValue !=null) {setState(() { logWeight = parsedValue;});
} else {}
},),
TextFormField(keyboardType:TextInputType.number,
onChanged:(value) {
final int?parsedValue = int.tryParse(value);
if (parsedValue != null) {setState(() {logRepetitions = parsedValue;});
} else {}},),
OutlinedButton(child: Text('Add Log'),
onPressed:
() async {
final Map<String, dynamic> arguments = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
final int id = arguments['id'] ??0;
final String logWorkout = arguments['logWorkout'] ??'';
final String logExercise = snapshot.data![index].name; // <-- set logExercise here
final int logOrder = breakdown.order;
print("$logRepetitions, $logWeight, $logOrder, $logExercise, $logWorkout, $id");
print(id.runtimeType);
try {
final http.Response response = await APIService.addLog(
logWeight, logRepetitions, logOrder, logExercise, logWorkout, id);
if (response.statusCode == 200) {
print('Log submitted successfully');} else {
print('Failed to submit log');}
} catch (error) { print(error);
await showDialog( context: context, builder: (context) {
return AlertDialog(title:Text('Error'),
content: Text(error.toString()),
actions: [ OutlinedButton(
child: Text('OK'),
onPressed: () {Navigator.of(context).pop();
},),],); },); } }, ), ],),),),
So in the dart file there are 2 textfields and an outline button inside a form. The textfields are where users add their inputs in numeric values and when the user clicks on the submit I receive this error. I have made sure that the only 2 strings which are logExercise, logWorkout
are actually strings. I am not sure what I am doing wrong and how to fix it. I am fairly new to flutter.
2
Answers
You need to convert your
int
variables toString
to use them insidehttp
body
Before all please put all static value to check if this work or not then check below.
and Also Runtime datatype all variables.