skip to Main Content

I have two pages and I want to use ‘id’ variable in second screen to fetch data from API.

What do I have to do?

Screen one: this is a screen that contains a list of cards and on each card there is a button to enter the detail page (second screen)

Screen two: I am showing data for this user by id

NB: when I send it I always get a 500 Internal Server Error because the ID was not sent, but if I write the ID directly in my code in Future the data appears.

screen one

TextButton(
                            onPressed: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => DetailAbsensi(
                                id: " ${data[index].idKelasKuliah}",
                              ),
                                ),
                              );
                            },
                            style: TextButton.styleFrom(
                              backgroundColor: const Color(0xffC9F7F5),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(8),
                              ),
                            ),
                            child: Text(
                              "Absensi",
                              style: bold5.copyWith(
                                color: const Color(
                                  0xff1BC5BD,
                                ),
                              ),
                            ),
                          ),

screen two

const DetailAbsensi({
    super.key,
    this.id = '',
  });
  final String id;

not work error 500 internal server

 Widget headerDetail() {
    return FutureBuilder<ModelAbsensi>(
      future: AbsensiProvider().getAbsensi(id), // not work error 500 internal server error
      builder: (context, snapshot) {
        print('ini adalah $id');
        if (snapshot.hasData) {

work

Widget headerDetail() {
    return FutureBuilder<ModelAbsensi>(
      future:
          AbsensiProvider().getAbsensi('e4d783cd-80a7-4419-9a81-e31e607afba8'), // work
      builder: (context, snapshot) {
        print('ini adalah $id');

fetch ID

Future<ModelAbsensi> getAbsensi(String id) async {
    String url = Constant.baseURL;
    String token = await UtilSharedPreferences.getToken();
    final response = await http.get(
      Uri.parse(
        '$url/auth/mhs_siakad/absensi_perkuliahan?id_kelas_kuliah=$id',
      ),
      headers: {
        'Authorization': 'Bearer $token',
      },
    );

    print(response.statusCode);
    print(response.body);
    if (response.statusCode == 200) {
      return ModelAbsensi.fromJson(jsonDecode(response.body));
    } else {
      throw Exception();
    }
  }

ID sent, but when you want to GET API based on ID an error occurs
enter image description here

2

Answers


  1. you are having an extra space before you are passing the id … try once by removing that.

    Login or Signup to reply.
  2. Note the space between the " and the $, try removing it

    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => DetailAbsensi(
                id: " ${data[index].idKelasKuliah}", // Note the space between the " and the $
            ),
        ),
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search