skip to Main Content

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.

enter image description here

respons failed update data

enter image description here

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


  1. void postData() async {
        final response = await http.post(Uri.parse(uri), body: {
          "title": "Anything",
          "body": "Post body",
          "userid": "1",
        });
        debugPrint(response.statusCode.toString());
      }
    
    Login or Signup to reply.
  2. It is possible the response.statusCode is not 201 but 200 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 (response.statusCode ~/100 == 2) {
          return UserBiodata.fromJson(jsonDecode(response.body));
        } else {
          print(response.statusCode)
          print(response.body)
          throw Exception('Failed update data!');
        }
    
    Login or Signup to reply.
  3. If the requirement is it doesn’t allow null values , then in your Model it would be preferred if you use non-null types.

    Example:

    //String? stAgama,
    String stAgama, // Remove nullable operator
    String stKwn,   
    
    ....
    String <non_nullable_variable>
    

    And even make sure you fill the String types with empty string if null in jsonEncode

    Example:

     jsonEncode(<String, dynamic>{
            "st_agama": stAgama ?? '', // assigning empty string if null
            'st_kwn': stKwn ?? '',
           
            '<non_nullable_variable>': <null_value> ?? ''
    
    Login or Signup to reply.
  4. if you are facing null exception due to passing null parameters, you can check null values in below map.

    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>{
            if(stAgama!=null)
            "st_agama": stAgama,
            
            if(stKwn!=null)
            'st_kwn': stKwn,
            // 'kwn': kwn,
            if(tempat!=null)
            "tempat": tempat,
            if(tglLahir!=null)
              "tgl_lahir": tglLahir,
            if(nik!=null)
              "nik": nik,
            if(nisn!=null)
              "nisn": nisn,
            if(npwp!=null)
              "npwp": npwp,
            if(jalan!=null)
              "jalan": jalan,
            if(stProv!=null)
              "st_prov": stProv,
            // 'province': province,
            if(stKab!=null)
              "st_kab": stKab,
            // 'kab': kab,
            if(stKec!=null)
              "st_kec": stKec,
            // 'kec': kec,
            if(desa!=null)
              "desa": desa,
            if(dusun!=null)
              "dusun": dusun,
            if(rt!=null)
              "rt": rt,
            if(rw!=null)
              "rw": rw,
            if(kodePos!=null)
              "kode_pos": kodePos,
            if(telp!=null)
              "telp": telp,
            if(email!=null)
              "email": email,
            if(kps!=null)
              "kps": kps,
            if(stTrans!=null)
              "st_trans": stTrans,
            if(stJn!=null)
            "st_jn": stJn,
            // "jn": jn,
            if(ipk!=null)
              "ipk": ipk,
            // "smt": smt,
            if(namaAyah!=null)
              "nama_ayah": namaAyah,
            if(nikAyah!=null)
              "nik_ayah": nikAyah,
            if(tglLahirAyah!=null)
              "tgl_lahir_ayah": tglLahirAyah,
            if(stPendidikanAyah!=null)
              "st_pendidikan_ayah": stPendidikanAyah,
            // "pendidikan_ayah": pendidikanAyah,
            if(pekerjaanAyah!=null)
              "pekerjaan_ayah": pekerjaanAyah,
            if(penghasilanAyah!=null)
              "penghasilan_ayah": penghasilanAyah,
            if(namaIbu!=null)
              "nama_ibu": namaIbu,
            if(nikIbu!=null)
              "nik_ibu": nikIbu,
            if(tglLahirIbu!=null)
              "tgl_lahir_ibu": tglLahirIbu,
            if(stPendidikanIbu!=null)
              "st_pendidikan_ibu": stPendidikanIbu,
            
            // "pendidikan_ibu": pendidikanIbu,
            if(pekerjaanIbu!=null)
              "pekerjaan_ibu": pekerjaanIbu,
            if(penghasilanIbu!=null)
              "penghasilan_ibu": penghasilanIbu,
            if(namaWali!=null)
              "nama_wali": namaWali,
            if(nikWali!=null)
              "nik_wali": nikWali,
            if(tglLahirWali!=null)
              "tgl_lahir_wali": tglLahirWali,
            if(stPendidikanWali!=null)
              "st_pendidikan_wali": stPendidikanWali,
            // "pendidikan_wali": pendidikanWali,
            if(pekerjaanWali!=null)
              "pekerjaan_wali": pekerjaanWali,
            if(penghasilanWali!=null)
              "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!');
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search