我有问题,我想使用 POST API 方法发送数据。这里我有几个必须发送的参数,分别是list、string、int、date time等形式。
我已经尝试了 flutter 文档中的示例并且它有效,因为它只发送字符串参数,但在我的情况下,我必须发送大量具有不同数据类型的数据。填null会报错,因为所有的表格都需要从服务端填写
我想知道这里有没有人遇到过类似的情况,或者提供一个如何使用POST API方法发送数据的例子。我非常沮丧和困惑,因为我已经好几天没有完成它了,我试着寻找它,但没有找到与我目前正在经历的情况相关的案例。 请帮忙 谢谢你。
响应更新数据失败
发布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!');
}
}
按钮发送
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'),
),
),
response.statusCode
可能不是201
而是200
。我们需要检查状态码是什么。请使用以下代码代替,如果不起作用,请粘贴结果:
if (response.statusCode ~/100 == 2) {
return UserBiodata.fromJson(jsonDecode(response.body));
} else {
print(response.statusCode)
print(response.body)
throw Exception('Failed update data!');
}
如果要求不允许
null
值,那么在您的 Model
中,如果您使用 non-null
类型,那将是首选。
例子:
//String? stAgama,
String stAgama, // Remove nullable operator
String stKwn,
....
String <non_nullable_variable>
如果
String
在 null
中,甚至确保用空字符串填充
jsonEncode
类型
例子:
jsonEncode(<String, dynamic>{
"st_agama": stAgama ?? '', // assigning empty string if null
'st_kwn': stKwn ?? '',
'<non_nullable_variable>': <null_value> ?? ''
void postData() async {
final response = await http.post(Uri.parse(uri), body: {
"title": "Anything",
"body": "Post body",
"userid": "1",
});
debugPrint(response.statusCode.toString());
}