Error when selecting a dropdown item in Flutter

To complete the rmtmckenzie answer, here is the code you should put in your FeedCategory object:

bool operator ==(o) => o is FeedCategory && o.name == name;
int get hashCode => name.hashCode;

Edit: A link that explains what hashCode is made for in Flutter


I think I've figured out your issue. It stems from how you're using FutureBuilder.

This is what I think is going on:

  1. Your app is runs. It does the call to getFeedCategories()
  2. The future completes, and the list is built, etc with the following items:

    obj 123 ==> FeedCategory(Prueba), obj 345 ==> FeedCategory(Categories 2)

  3. You select the item from the dropdown, setState() is called

    Your _feedCategory is now equal to obj 123 ==> FeedCategory(Prueba)

  4. Widget is rebuilt. It does another call to getFeedCategories()

  5. Future completes, list is built etc with the following items

    obj 567 ==> FeedCategory(Prueba), obj 789 ==> FeedCategory(Categories 2)

  6. The Dropdown tests items.where((DropdownMenuItem item) => item.value == value).length == 1, but length == 0 because obj 123 ==> FeedCategory(Prueba) is not found.

There are a couple of solutions to your problem. One would be to add an equals operator to your FeedCategory class that compares the category and/or id.

Another would be to separate the Future used in the futurebuilder from the part that changes - you could do this either by keeping the future as a member variable (maybe instantiate it in initState?), or by making the inner part of the builder into it's own Stateful widget (in which case you could probably make ManageFeedSource a StatelessWidget). I'd recommend the last option.

Let me know if that doesn't solve your issue, but I'm pretty sure that's the reason it's doing what it's doing.