0

i am getting values from server to dropdown which are inserted previous from static list of dropdown values, but i need to use dropdown when value from server is 'Pending' to update specific record, below my code.

  List<String> approvalList = ['Pending', 'Approve', 'Discard'];
  String dropdownValue="Pending";

 Container(
                  height: MediaQuery.of(context).size.height*0.3,
                  width: MediaQuery.of(context).size.width,
                  child:StreamBuilder<List<ApprovalModel>>(
                    stream: bloc.approvalsStream,
                    initialData: [],
                    builder: (context, snapshot) {
                      return ListView.builder(
                          itemCount: snapshot.data!.length,
                          itemBuilder: (context,i){
                            return snapshot.connectionState==ConnectionState.waiting?Lottie.asset(
                              'assets/lottieloading.json',
                              width: 70,
                              height: 70,
                              fit: BoxFit.fill,
                            ):ListTile(
                              title: Text(snapshot.data![i].approverName),
                              trailing: StatefulBuilder(
                                  builder: (BuildContext context, StateSetter setState) {

                                   return DropdownButton<String>(

                                        value: snapshot.data![i].status==0?'Pending':
                                        snapshot.data![i].status==1?'Approve':
                                        'Discard',



                                        items: approvalList.map((String val) {
                                          return DropdownMenuItem<String>(
                                            value: val,
                                            child: new Text(val),
                                          );
                                        }).toList(),
                                        hint: Text(selectedValue),


                                        onChanged: (val) {
                                          setState(() {
                                            dropdownValue = val!;
                                          });
                                        });
                                  }
                              ),
                            );
                          });
                    }
                  )
                  ,
                ),

As You see i am setting value from server it is working fine, but when the value is pending i want to use the dropdown to update record in database.

Muhammad Umair
  • 274
  • 3
  • 13
  • According to your statement, you want to update database using `DropDown`when it is on pending state. Does it mean you like to update db only after getting `snapshot.data![i].status==0?'Pending':` or you like to update after selecting from DropDownMenu? – Md. Yeasin Sheikh Nov 06 '21 at 08:38
  • I need both, update and state change of dropdownbutton. – Muhammad Umair Nov 08 '21 at 04:08

1 Answers1

0

At onChanged when you update your dropdownValue , also call the method you are using to update record in database.

Inan Mahmud
  • 226
  • 3
  • 14
  • I already tried this but i want dropdown to change its state. – Muhammad Umair Nov 06 '21 at 08:16
  • doesn't the value get updated when you use setState((){}) ? – Inan Mahmud Nov 06 '21 at 08:21
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Tyler2P Nov 06 '21 at 10:42