1

I know that there is many ways / package to implement multi select in dropdownbutton in flutter like --> this one But with my little knowledge, I want to reinvent the wheel for basic building!!!

My scenario ->

I have a list of location's in json format like this -->

[{id: 928, location: Amtoli}, {id: 905, location: Ashok Tala}, {id: 899, location: Badur Tola}]

and two List -->

List _location = new List(); // this comes from API;
List _multiSelectLoc = new List();

And in DropDownButton's onChanged property -->

    onChanged: (newValue) {
setState(() {
  _location.forEach((e) {
  if (e["id"] == newValue.toString()) {
    _multiSelectLoc.add(e);
    print(_multiSelectLoc);
  }
});
  _location.removeWhere(
      (e) => e['id'] == newValue.toString());
  print(_location);
});
},

I am curious to know why my code was not working, why I can't remove data from List _location, and and add to List _multiSelectLoc

I already simulated such condition in dartpad and it's just woking fine! enter image description here

xahid_rocks
  • 632
  • 8
  • 24

1 Answers1

0

I guess I found the problem,

rewritten code -->

onChanged: (newValue) {
                      _location.forEach((e) {
                        if (e['id'].toString() == newValue.toString()) {
                          _multiSelectLoc.add(e);
                          // print(_multiSelectLoc);
                        }
                      });
                      _location.removeWhere(
                          (e) => e['id'].toString() == newValue.toString());
                      setState(() {
                        // print(_location);
                      });
                    },

I think the problem was, I was using a dynamic type to compare with string type, when I added .toString() the code started to giving me the results!

xahid_rocks
  • 632
  • 8
  • 24