Home > Back-end >  Flutter: How to obtain text entered from Autocomplete field instead of selecting a suggestion?
Flutter: How to obtain text entered from Autocomplete field instead of selecting a suggestion?

Time:12-14

Flutter provides an Autocomplete widget. If I enter "abc" instead of selecting one of the suggestions, then how can I get this text "abc"?

CodePudding user response:

You can create another variable to hold the input String inside state class like String? _inputString; and update value inside optionsBuilder

class _AutocompleteBasicExampleState extends State<AutocompleteBasicExample> {
  String? _inputString;

  @override
  Widget build(BuildContext context) {
    return Autocomplete<String>(
      optionsBuilder: (TextEditingValue textEditingValue) {
        setState(() {
          _inputString = textEditingValue.text;
        });
  • Related