Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting selectedItem property during build() is broken in version 0.4.6 #56

Closed
slavap opened this issue Oct 31, 2020 · 8 comments
Closed

Comments

@slavap
Copy link

slavap commented Oct 31, 2020

In 0.4.5 it is working fine, updating dependency to 0.4.6 - stops working, returning back to 0.4.5 - working again.

@salim-lachdhaf
Copy link
Owner

Do you mean setdefault selected item not working anymore ?

@slavap
Copy link
Author

slavap commented Oct 31, 2020

@salim-lachdhaf yes. selectedItem set initially during build() is not working in 0.4.6 anymore.

@salim-lachdhaf
Copy link
Owner

it working just fine, please re check

@slavap
Copy link
Author

slavap commented Nov 1, 2020

@salim-lachdhaf I’ve checked multiple times. Not working. Will prepare demo project, my code is not that simple, probably combination of other used properties affects it.

@salim-lachdhaf
Copy link
Owner

That is could be. Thank you

@slavap
Copy link
Author

slavap commented Nov 3, 2020

@salim-lachdhaf
Please see attached project. It is on 0.4.5 and when you run it in dropdown there will be MIA value. Press 'Set LIH' button and value in dropdown will change to LIH.
Then change to version 0.4.6 and do flutter clean.
Run and repeat steps -> value in dropdown won't change to LIH and remains MIA.

I'm testing on beta channel, Flutter 1.23.0-18.1.pre

dd_bug.zip

@salim-lachdhaf
Copy link
Owner

salim-lachdhaf commented Nov 3, 2020

it's not a bug. in fact it was a bug in 0.4.5.
explanation:
in 0.4.5 we had this function

@override
 void didUpdateWidget(DropdownSearch<T> oldWidget) {
   super.didUpdateWidget(oldWidget);
   _selectedItemNotifier.value = widget.selectedItem;
 }

which cause a lot of problems for users, for example losing selectedItem (back to default one) in keyboard popup, change focus ....
and a lot of users uses the setState as manner to change the selectedItem programmatically WHICH IS A WRONG WAY, because the default selected item is INITIALISATED ONES in initState()

Now in 0.4.6 i removed the bug (function that re set default selected value) and i added a proper way to set the selected value programmatically using GlobalKey as showed in the example of the plugin.
in Addition, in 0.4.6 you don't have anymore to call setState() to build all your widget just to change your selected item , 0.4.6 will increase the performance of your app.

thank you for reporting your notice, Waiting for your feedback after updating your app based on 0.4.6 changes ;)

Edit:

i can do something like :

@override
void didUpdateWidget(DropdownSearch<T> oldWidget) {
  if (oldWidget.selectedItem != widget.selectedItem) {
    _selectedItemNotifier.value = widget.selectedItem;
  }
  super.didUpdateWidget(oldWidget);
}

and your problem will be solved either in the version 0.4.6. but i prevent the uses of unnecessary setState() call.
maybe in the next version i will think about adding it our not, but it remains a bad solution in all cases.

@slavap
Copy link
Author

slavap commented Nov 6, 2020

@salim-lachdhaf Thanks for explanation, I agree that setState() call is much worse than using _ddKey.currentState.changeSelectedItem(). After I've changed my code to using keys it's all works. The only complain - please next time on pub.dev mark such changes as BREAKING, because it was absolutely not obvious that refactoring is needed all over my project, and actually such refactoring took some time, not as easy as 5 minutes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants