jifalops / simple_autocomplete_formfield

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

itemFromString Heisenbug (Linux Desktop Mode) - Fails when there isn't a breakpoint #15

Open SecareLupus opened 2 years ago

SecareLupus commented 2 years ago

When building for Linux Desktop, I'm hitting a bug that disappears when I try to investigate it. First, when clicking on a suggestion, the function argument passed into itemFromString triggers, when the documentation for itemFromString says it triggers:

when the input loses focus and a suggestion was not selected

in any case, I've set a logpoint breakpoint in the function argument to track down what's happening.

When I disable the breakpoint, the onTap event on the suggestion never fires, because it calls itemFromString once, and onChanged once, with the textfield's data as the argument.

When I enable the breakpoint, it again calls itemFromString and onChanged once each, as when disabled, but after looping and logging in itemFromString, it then fires onChanged again, with the tapped suggestion as the argument.

It's like the itemFromString function is finishing so quickly that it's interrupting the onTap event somehow...

`itemFromString: (string) {

return SUBSTANCES.firstWhere((_sub) {

    return _sub.toLowerCase() == string.toLowerCase();

}, orElse: () => string);

}`

Where SUBSTANCES is a List containing 339 entries. It's important to note that my logpoint is on the second return statement, so it triggers 339 times. Replacing the logpoint with print statements does not cause the bug to disappear, it seems to require a breakpoint of some sort.

While logging output with print statements, I always get 2x flutter: onChanged: value=null upon my initial click into the formfield. (print statement)

My keyboard input cause no itemFromString or onChanged calls, but as soon as I click a suggestion I get 339x flutter: itemFromString: false (breakpoint log or print log both cause this)

The next thing that happens is a call to onChanged with the text from the form field. This is followed by a call to onChanged with the value of the clicked suggestion, if and only if the breakpoint is enabled in the body of the firstWhere() test function argument. If the breakpoint is disabled, the final call to onChanged never happens.

Because of the strange nature of the bug, I've provided as much information as I can, though I wonder if the issue is a Flutter bug when compiling to Linux Desktop, instead of a bug in your project, since it appears to be a weird race condition or something to do with taps acting differently across platforms. Either way, I thought it best to submit a ticket here first. I'm happy to provide any additional information I can if it will help.

Additional platform note, the same code runs just fine on Android, and only appears to call onChanged once, passing the value of the selected suggestion. It does not call itemFromString when tapping on a suggestion.