0

I have stateless class contains form with 4 tabs and one of those tabs contains 3 dropdowns to select address on for country and based on value selected I want the second dropdown (which is for cities) to view its items. all items for dropdowns comes from local sqlite database. the problem is the second dropdown dose not view its items but when I use debugger I found the list comes from database successfully but the update of list value on stateless class not happen. any way to solve this??

DropdownButtonFormField<Region>(
                                decoration: InputDecoration(
                                  isDense: true,
                                  floatingLabelBehavior:
                                  FloatingLabelBehavior.auto,
                                  labelStyle: TextStyle(fontSize: 22),
                                  contentPadding:
                                  EdgeInsets.symmetric(vertical: 9),
                                ),
                                value: regionList.isEmpty
                                    ? region
                                    : helRegion.getReg(
                                    user.regionAdresse, regionList),
                                //icon: Icon(Icons.arrow_downward),
                                iconSize: 24,
                                elevation: 16,
                                onChanged: (selectedRegion) {
                                   onRegionSelected(context, user, 
                       region, province, provinceList, selectedRegion);
                                },
                                items: regionList
                                    .map((project) => DropdownMenuItem(
                                  child: Text(project.Name),
                                  value: project,
                                ))

here is onRegionSelected function:

 onRegionSelected(BuildContext context, User user, Region region, Province province, List<Province> provinceList, Region selectedRegion) async {try { showLoadingDialog(context);
final _provinceList = await getProvinceList(selectedRegion.id);

  region = selectedRegion;
  user.regionAdresse = selectedRegion.id;
  province = null;
  //provinceList.clear();
  provinceList = _provinceList;

Navigator.pop(context);} catch (e) {
//TODO: handle error
rethrow; }}
5
  • Welcome to SO! Would you please edit your question and copy-paste the onRegionSelected function? Commented Jul 19, 2022 at 14:53
  • ok, i did that .. please help if you can. Commented Jul 21, 2022 at 8:34
  • All those changes in the internal state on onRegionSelected should be inside a setState(() { ...here... }). Otherwise, there is no repaint/refresh of the screen. Commented Jul 21, 2022 at 9:14
  • so i can not use stateless class, it should be statefull .. is that what you mean? Commented Jul 21, 2022 at 12:53
  • Yes, that's it. You need a StatefulWidget if you have states that change internally. Commented Jul 21, 2022 at 13:51

1 Answer 1

1

First of all, you should use a stateful class so that when you select the first dropdown, the information that will appear in the second one is updated, and so on. If your idea is to keep the format stateless, you could also save the responses from the DB in a stream and wrap the dropdowns in streamBuiler, so they could be updated when the new data pointed to by each dropdown is updated. I'm leaving this as an answer since I don't have the reputation to add it as a comment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.