'How can I use a set state without stateful widget and I am using a search delegate in flutter/Dart?
what I want to do is that my search history can update it, I can´t delete a previous search with click on the delete icon but I want to use the set state but it gives me an error and the reason may be that I am not using a StatefulWiget because I am using a Search delegate. I would appreciate the help. Code::
class SearchDelegateMenuPrincipal extends SearchDelegate<SearchDelegateModel>{
@override
final String searchFieldLabel;
List<SearchDelegateModel> historialMenuPrincipal=[];
SearchDelegateModel busquedaa;
final menuprincipalservices = new MenuPrincipalServices();
SearchDelegateMenuPrincipal(this.searchFieldLabel,this.historialMenuPrincipal);
@override
List<Widget> buildActions(BuildContext context) {
//code
}
@override
Widget buildLeading(BuildContext context) {
return Container(
//code
),
);
}
@override
Widget buildResults(BuildContext context) {
//code
}
@override
Widget buildSuggestions(BuildContext context) {
return HistorialBusquedaSuggestions(this.historialMenuPrincipal );
}
Widget HistorialBusquedaResults(List <SearchDelegateModel> BusquedaHistorial ){
//code
}
Widget HistorialBusquedaSuggestions(List <SearchDelegateModel> historialMenuPrincipal ){
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
color: Colors.white
),
child: ListView.builder(
itemCount: historialMenuPrincipal.length,
itemBuilder: (context,i){
contentPadding: EdgeInsets.symmetric(vertical: 12,horizontal: 16);
leading:CircleAvatar(
radius: 32,
backgroundImage: NetworkImage(
"https://9.bp.blogspot.com/-3ZzNZsjQk/WR94I4II/AAAAAAAAAJw/_inTVynS60V75IZ-461-paWArTSwCEw/s1600/ANA.jpg"),
);
return ListTile(
title: Text(historialMenuPrincipal[i].email?? "no hay data"),
trailing: IconButton(
icon: Icon(Icons.cancel,color: Colors.black,),
onPressed: () {
setState((context) {
busqueda.remove(busqueda[]); // i want to use this
});
},
),
);
}
),
);
}
Solution 1:[1]
No, That's the whole point of Stateless
and Stateful
widgets. Only Stateful
has this setState()
There's some other method, which goes like:
(context as Element).markNeedsBuild();
Pass this at some button press, although its from flutter 1.x something version.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Hamza |