Dynamic suggestions
tezine opened this issue · 5 comments
Hi, is it possible to use a dynamic suggestion list?
The idea is to retrieve a suggestion list from the backend based on what's typed by the user, so whenever the text changes, a new list is filled with suggestions.
I'm using Consumer and Provider, updating the suggestion list onPointerDown
, and calling notifyListeners
after the retrieval of new items, but they are not displayed as suggestions.
Is there anyway to contour this issue?
I know it's been a month, but this might help. Use a global key assigned to the autocomplete textfield, e.g. _autoKey. Use updateSuggestions, like this: _autoKey.currentState.updateSuggestions(List suggestions);
can you give complete code sample having same issues
- Define a variable to hold your AutoCompleteTextField instance, e.g.
AutoCompleteTextField autoComplete;
- In
Widget build(Build context)
setautoComplete
variable toAutoCompleteTextField
instance - Use the
autoComplete.updateSuggestions(suggestions)
method wheneversuggestions
array is updated
That worked for me.
con respuesta muy pobres le voy a pasa la solución definitiva, hay que pensar como no conocemos el código
- tiene que añadir el key
GlobalKey<AutoCompleteTextFieldState<String>> keyAutoCompleteText = new GlobalKey();
la llave keyAutoCompleteText se declara al inicio del componete - llama el método para actualiza la lista
keyAutoCompleteText.currentState.updateSuggestions(listaPlaca);
en este caso de uso use un método obtenerListaPlaca() este me actualiza la información y ejecuto el
keyAutoCompleteText.currentState.updateSuggestions(listaPlaca);
ejemplo completo:
class InspeccionDiariaVehiculoForm extends StatefulWidget {
@override
InspeccionSstRcoFormState createState() => InspeccionSstRcoFormState();
}
class InspeccionSstRcoFormState extends State<InspeccionDiariaVehiculoForm> {
GlobalKey<AutoCompleteTextFieldState<String>> keyAutoCompleteText = new GlobalKey();
Future<List<String>> obtenerListaPlaca() async {
listaPlaca = await vehiculoPlacaDB.getDataList();
keyAutoCompleteText.currentState.updateSuggestions(listaPlaca);
}
}
Widget buildTextFielPlaca() {
return SimpleAutoCompleteTextField(
controller: controllerPlaca,
suggestions: listaPlaca,
decoration: InputDecoration(
labelText: "Placa",
),
key: keyAutoCompleteText,
onFocusChanged: (hasFocus) {},
textSubmitted: (text) {
//_controller_tipo_unidad.text = text;
},
clearOnSubmit: false,
);
}
This can be done, just update the suggestions list when the backend request is completed.