I’m having a problem, I want to send data using the POST API method. Here I have several parameters that must be sent, namely in the form of list, string, int, date time and others.
I’ve tried the example from the flutter documentation and it works because it only sends parameters which are Strings, but in my case I have to send quite a lot of data with different data types. and there is an error if it is filled with null, because all forms are required to be filled from the server.
I want to know if anyone here has experienced a similar case or provide an example of how to send data using the POST API method. I am very frustrated and confused because I haven’t finished it for days, I tried looking for it but haven’t found a case with what I’m currently experiencing.
Please help
Thank You.
respons failed update data
Post API
Future<UserBiodata> sendBiodata(
String? stAgama,
// List<Agama>? agama,
String? stKwn,
// List<Kwn>? kwn,
String? tempat,
String? tglLahir,
String? nik,
String? nisn,
String? npwp,
String? jalan,
String? stProv,
// List<Province>? province,
String? stKab,
// List<Province>? kab,
String? stKec,
// List<Province>? kec,
String? desa,
String? dusun,
String? rt,
String? rw,
String? kodePos,
dynamic telp,
String? email,
String kps,
String? stTrans,
// List<Trans>? trans,
String? stJn,
// List<Jn>? jn,
String? ipk,
// int? smt,
String? namaAyah,
String? nikAyah,
String? tglLahirAyah,
String? stPendidikanAyah,
// List<PendidikanAyah>? pendidikanAyah,
String? pekerjaanAyah,
String? penghasilanAyah,
String? namaIbu,
String? nikIbu,
String? tglLahirIbu,
String? stPendidikanIbu,
// List<PendidikanIbu>? pendidikanIbu,
String? pekerjaanIbu,
String? penghasilanIbu,
String? namaWali,
String? nikWali,
String? tglLahirWali,
String? stPendidikanWali,
// List<PendidikanWali>? pendidikanWali,
String? pekerjaanWali,
String? penghasilanWali,
) async {
String url = Constant.baseURL;
String token = await UtilSharedPreferences.getToken();
final response = await http.post(
Uri.parse(
'$url/auth/mhs_siakad/biodata/update',
),
headers: <String, String>{
'Authorization': 'Bearer $token',
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, dynamic>{
"st_agama": stAgama,
// 'agama': agama,
'st_kwn': stKwn,
// 'kwn': kwn,
"tempat": tempat,
"tgl_lahir": tglLahir,
"nik": nik,
"nisn": nisn,
"npwp": npwp,
"jalan": jalan,
"st_prov": stProv,
// 'province': province,
"st_kab": stKab,
// 'kab': kab,
"st_kec": stKec,
// 'kec': kec,
"desa": desa,
"dusun": dusun,
"rt": rt,
"rw": rw,
"kode_pos": kodePos,
"telp": telp,
"email": email,
"kps": kps,
"st_trans": stTrans,
// 'trans': trans,
"st_jn": stJn,
// "jn": jn,
"ipk": ipk,
// "smt": smt,
"nama_ayah": namaAyah,
"nik_ayah": nikAyah,
"tgl_lahir_ayah": tglLahirAyah,
"st_pendidikan_ayah": stPendidikanAyah,
// "pendidikan_ayah": pendidikanAyah,
"pekerjaan_ayah": pekerjaanAyah,
"penghasilan_ayah": penghasilanAyah,
"nama_ibu": namaIbu,
"nik_ibu": nikIbu,
"tgl_lahir_ibu": tglLahirIbu,
"st_pendidikan_ibu": stPendidikanIbu,
// "pendidikan_ibu": pendidikanIbu,
"pekerjaan_ibu": pekerjaanIbu,
"penghasilan_ibu": penghasilanIbu,
"nama_wali": namaWali,
"nik_wali": nikWali,
"tgl_lahir_wali": tglLahirWali,
"st_pendidikan_wali": stPendidikanWali,
// "pendidikan_wali": pendidikanWali,
"pekerjaan_wali": pekerjaanWali,
"penghasilan_wali": penghasilanWali,
}),
);
print(response.statusCode);
print(response.body);
if (response.statusCode == 201) {
return UserBiodata.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed update data!');
}
}
button send
Center(
child: ElevatedButton(
onPressed: () {
setState(() {
_futureBiodata = BiodataProvider().sendBiodata(
_controllerAgama.text,
// _controller2.text as List<Agama>?,
_controllerKwn.text,
// _controller4.text as List<Kwn>?,
_controllerTmtLahir.text,
_controllerTglLahir.text,
_controllerNik.text,
_controllerNisn.text,
_controllerNpwp.text,
_controllerAlamat.text,
_controllerProv.text,
// _controller12.text as List<Province>?,
_controllerKab.text,
// _controller14.text as List<Province>?,
_controllerKec.text,
// _controller16.text as List<Province>?,
_controllerKel.text,
_controllerDusun.text,
_controllerRt.text,
_controllerRw.text,
_controllerKdPos.text,
_controllerNoHp.text,
_controllerEmail.text,
_controllerKps.text,
_controllerNoKps.text,
// _controller26.text as List<Trans>?,
_controllerTrans.text,
// _controller28.text as List<Jn>?,
_controllerJnsTinggal.text,
// _controller30.text as int?,
_controllerNmAyah.text,
_controllerNikAyah.text,
_controllerTglLhrAyah.text,
_controllerPendidikanAyah.text,
// _controller35.text as List<PendidikanAyah>?,
_controllerPekerjaanAyah.text,
_controllerPenghasilanAyah.text,
_controllerNmIbu.text,
_controllerNikIbu.text,
_controllerTglLhrIbu.text,
_controllerPekerjaanIbu.text,
// _controller42.text as List<PendidikanIbu>?,
_controllerPekerjaanIbu.text,
_controllerPenghasilanIbu.text,
_controllerNmWali.text,
_controllerNikWali.text,
_controllerTglLhrWali.text,
_controllerPendidikanWali.text,
// _controller49.text as List<PendidikanWali>?,
_controllerPekerjaanWali.text,
_controllerPekerjaanWali.text,
);
});
},
child: const Text('Update Data'),
),
),
4
Answers
It is possible the
response.statusCode
is not201
but200
instead. We need to check what’s the status code. Please use the following code instead and paste the results if it doesn’t work:If the requirement is it doesn’t allow
null
values , then in yourModel
it would be preferred if you usenon-null
types.Example:
And even make sure you fill the
String
types with empty string ifnull
injsonEncode
Example:
if you are facing null exception due to passing null parameters, you can check null values in below map.