felixmccuaig / flutter-autocomplete-textfield

An autocomplete Textfield for flutter
MIT License
181 stars 131 forks source link

Using multiple autoComplete Textfield in same widget tree cause global key exception #6

Closed pranayairan closed 5 years ago

pranayairan commented 5 years ago

Hi thanks for this amazing library, I was having an issue while using it and want to check if anyone knows how to solve.

I want to use the 2-3 autocomplete text fields on the same screen. But when I add it and run the app, I get the error Multiple widgets used the same GlobalKey.

This happens because the global key which is a required field cannot be used for more than 1 widget. There is a workaround by declaring our own key https://stackoverflow.com/questions/49862572/multiple-widgets-used-the-same-globalkey but autocomplete textfield do a type check and the above solution doesn't work.

For my use case, I am not using clear, is it possible to not make key required for such use cases? or is there any other suggestion.

felixmccuaig commented 5 years ago

Umm yeah, this should most definitely be possible. Just give each autocomplete text field its own global key and you'll be fine.

GlobalKey<AutoCompleteTextFieldState<String>> key1 = new GlobalKey(); //Key for textfield one GlobalKey<AutoCompleteTextFieldState<String>> key2 = new GlobalKey(); //Key for textfield two

pranayairan commented 5 years ago

I tried this but it throws an exception

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
2018-10-22 07:30:43.149 28048-28064/dexterapps.bankifscflutter I/flutter: The following assertion was thrown building
2018-10-22 07:30:43.149 28048-28064/dexterapps.bankifscflutter I/flutter: FancyAutoCompleteTextField-[LabeledGlobalKey<AutoCompleteTextFieldState<String>>#809e7](state:
2018-10-22 07:30:43.149 28048-28064/dexterapps.bankifscflutter I/flutter: _FancyAutoCompleteTextFieldState#47758):
2018-10-22 07:30:43.149 28048-28064/dexterapps.bankifscflutter I/flutter: Multiple widgets used the same GlobalKey.
2018-10-22 07:30:43.149 28048-28064/dexterapps.bankifscflutter I/flutter: The key [LabeledGlobalKey<AutoCompleteTextFieldState<String>>#809e7] was used by multiple widgets.
2018-10-22 07:30:43.149 28048-28064/dexterapps.bankifscflutter I/flutter: The parents of those widgets were:
2018-10-22 07:30:43.149 28048-28064/dexterapps.bankifscflutter I/flutter: - Semantics(container: false, properties: SemanticsProperties, label: null, value: null, hint: null,
2018-10-22 07:30:43.149 28048-28064/dexterapps.bankifscflutter I/flutter:   hintOverrides: null, renderObject: RenderSemanticsAnnotations#d5887 NEEDS-LAYOUT NEEDS-PAINT)
2018-10-22 07:30:43.149 28048-28064/dexterapps.bankifscflutter I/flutter: - Padding(padding: EdgeInsets(4.0, 0.0, 4.0, 0.0), renderObject: RenderPadding#e4d36 NEEDS-LAYOUT
2018-10-22 07:30:43.149 28048-28064/dexterapps.bankifscflutter I/flutter:   NEEDS-PAINT)
2018-10-22 07:30:43.149 28048-28064/dexterapps.bankifscflutter I/flutter: A GlobalKey can only be specified on one widget at a time in the widget tree.
felixmccuaig commented 5 years ago

2018-10-22 07:30:43.149 28048-28064/dexterapps.bankifscflutter I/flutter: Multiple widgets used the same GlobalKey. This line states multiple widgets are using the same key. here seems like a good overview of how globalkey works in Flutter.

pranayairan commented 5 years ago

thanks, I am actually using it in a diff way, i created another widget on top of autoComplete widget, due to this i am not able to pass multiple global keys, since doing GlobalKey<AutoCompleteTextFieldState> key1 = new GlobalKey() 2 times also create same instance of Globalkey.

felixmccuaig commented 5 years ago

If you're subclassing the AutoCompleteTextField, you'll need to implement the necessary implementations.