skip to Main Content

I have a case where I have to send the ID parameter to another page, and on another page I have to receive the ID that was sent on the first page.
on the first page I have API data in the form of a list and each card/data has a different ID, when the user chooses card A then what is sent is ID A and on another page must receive ID A, if the user chooses card B then what is sent is ID B and on other pages must accept ID B

i have two API

first the API data is a list and I want to send this ID.

enter image description here

and this is the second API where when accessing this API an ID must be sent.

enter image description here

how do i make the ID reusable without having to type it in the API because this code is hardcore code how do i make it so that when the user selects another card it automatically displays a different ID based on the user’s choice.

I’m confused here

page one

enter image description here

page two detail (get ID in page one)

enter image description here

Future<ModelAbsensi> getDetailAbs() 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=c92f49f7-cabf-423a-bfa5-ae798a3048d9',
      ),
      headers: {
        'Authorization': 'Bearer $token',
      },
    );
    print('${response.statusCode}');
    print(response.body);
    if (response.statusCode == 200) {
      final result = json.decode(response.body) as Map<String, dynamic>;
      Data detailKurikulum = Data.fromJson(result);
      ModelAbsensi? l = detailKurikulum.mataKuliah as ModelAbsensi?;
      return l!;
    } else {
      throw Exception();
    }
  }

button click

SizedBox(
              width: double.infinity,
              height: 38,
              child: TextButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: ((context) {
                        return DetailAbsensi(
                          jadwal: post,
                        );
                      }),
                      settings: RouteSettings(
                        arguments: todos,
                      ),
                    ),
                  );
                },
                style: TextButton.styleFrom(
                  backgroundColor: const Color(0xffC9F7F5),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8),
                  ),
                ),
                child: Text(
                  "Absensi",
                  style: bold5.copyWith(
                    color: const Color(
                      0xff1BC5BD,
                    ),
                  ),
                ),
              ),
            ),

2

Answers


  1. Check out how data can be passed from one page to another here
    https://docs.flutter.dev/cookbook/navigation/passing-data

    Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: ((context) {
                        return DetailAbsensi(
                          jadwal: post,
                        );
                      }),
                      settings: RouteSettings(
                        arguments: todos[index],//you have to specify index of list
                      ),
                    ),
                  );
    

    enter image description here

    Login or Signup to reply.
  2. follow this exmaple:

    you can run it on : https://dartpad.dev/

    import 'package:flutter/material.dart';
    
    const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            theme: ThemeData.dark().copyWith(
              scaffoldBackgroundColor: darkBlue,
            ),
            debugShowCheckedModeBanner: false,
            home: const AScreen());
      }
    }
    
    class AScreen extends StatefulWidget {
      const AScreen({Key? key}) : super(key: key);
    
      @override
      State<AScreen> createState() => _AScreenState();
    }
    
    class _AScreenState extends State<AScreen> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text("Screen A"),
          ),
          body: ListView.builder(
            itemCount: 20,
            itemBuilder: (context, index) => Card(
              child: Padding(
                padding: const EdgeInsets.all(20),
                child: Column(
                  children: [
                    Text("card $index"),
                    ElevatedButton(
                        onPressed: () {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => BScreen(id: "card $index"),
                              ));
                        },
                        child: const Text("Go to B"))
                  ],
                ),
              ),
            ),
          ),
        );
      }
    }
    
    Future<String> getData(String id) async {
      return Future.value("test abc with ID:   $id");
    }
    
    class BScreen extends StatelessWidget {
      const BScreen({Key? key, this.id = ''}) : super(key: key);
      final String id;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text("B Screen")),
          body: Center(child:FutureBuilder(
            future: getData(id),
            builder: (context, snapshot) => Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(snapshot.data.toString()),
              ],
            ),
          ),)
        );
      }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search