jifalops / simple_autocomplete_formfield

A Flutter widget that wraps a TextFormField and assists with autocomplete functionality.
MIT License
42 stars 16 forks source link

onChanged always return null value #16

Open emilhong opened 2 years ago

emilhong commented 2 years ago

Hi i am developer for using this dart package, currently now after i upgrade my flutter to 2.5.3, it no work on the onchanged side, it become alway return null value to me when i selected.Have any solution to this issue? flutter version: 2.5.3 dart version: 2.14.4 environment sdk: >=2.7.0 <3.0.0

Additional information, previously I used flutter 2.0.5 stable it still works, after upgrading it no work already. Thank you.

DragonSlayer88 commented 2 years ago

I second this. Please Fix this asap! It breaks everything.

SecareLupus commented 2 years ago

This appears to have the same cause as my issue #15 (and apparently most of the other issues too, now that I look), what platform are you attempting to run it on? It has worked properly for me on Android, but not on Linux. I've not yet tested other platforms.

It even happens in the example code; if you run the example/lib/main.dart in this project (at least on my Linux machine), click the name field, and click any autocomplete result, it will not update the field to match the selection.

However, if you add a debug breakpoint at line 48 (the onChanged named parameter), and run the example in debug mode, it will hit that breakpoint twice for each autocomplete option clicked, the first time passing the value null to onChanged, and the second time passing the selected value correctly. This causes the field to update properly! But disable that breakpoint, it stops working again.

Can either of you verify this as happening on your platforms as well? Also clarification of what platforms you're using would be great. I'm attempting to look deeper into this bug, maybe produce a fix, but I didn't write the library, so I'm still figuring out where to look.

SecareLupus commented 2 years ago

I've spent a couple hours now digging through code to try to work out how everything is stitched together. I've forked this off so I can investigate more freely, but it seems like the issue is resulting from something to do with either the onTap() function of individual suggestions or something to do with focus. Sometimes when tapping suggestions, it will fire onTap(), and sometimes it doesn't, and the only repeatable way to force it to fire is to enable a couple breakpoints.

Since it seems to be some sort of race condition that might be in the dart framework itself, and @emilhong mentioned that flutter 2.0.5 worked, while 2.5.3 failed, I took a look at the changelogs for Flutter itself between those versions, and found a couple merged PRs that may have some connection to the issues at hand.

In order of what I think is most likely to least likely:

Again, this is all preliminary, and tracking down why a bug should present only when breakpoints are not pausing operation, and only on some platforms is tricky. It seems like several of the functions that get called over the course of an interaction with an ACTFF are getting called the wrong number of times (usually too many, occasionally not at all), or being called with the wrong parameters (null when there should be values, most often). I will keep looking into this as time allows, but I'm not sure how optimistic I am at present.

The functions and variables I've noticed acting funny include the suggestion.onTap() function parameter, the tappedSuggestion state variable, the focusChanged() function parameter, the setValue function, and the _value getter. Some others may also be acting funny, but I'm still tracking things down, that's about how far I got today.

DragonSlayer88 commented 2 years ago

Hey any update on this? Seems to still be broken on the latest flutter stable build. Kinda lame. Using windows.

SecareLupus commented 2 years ago

Sorry, never worked this out, ended up switching to RawAutocomplete, which did what I needed to. This project has a number of anti-patterns, according to some flutter pros I talked to. I was only looking into this when I thought there wasn't a standard library option available.

josue487 commented 1 month ago

Hello, I found the solution just add a litle delay in focusChanged method touching the library code like this void focusChanged(bool focused) { if (focused) { setState(() { showSuggestions = state.controller.text.trim().length >= widget.minSearchLength; if (widget.resetIcon != null && state.controller.text.trim().isEmpty == showResetIcon) { showResetIcon = !showResetIcon; } }); } else { Future.delayed(Duration(milliseconds: 300), () { if (!state.focusNode.hasFocus) { setState(() => showSuggestions = false); setValue(_value); } }); } }