I have a provider called "FavoritesProvider" which has a variable called "_favoritos" where I store a list of id's.
What I am trying to do, is redraw a ListView widget every time a new item is added or removed to the favorites variable. Here is the function where I do that, where I update the variable and notifyListeners.
Future setFavorites(
int id,
) async {
final prefs = await SharedPreferences.getInstance();
String _receivedData = (prefs.getString('favorites') ?? "");
List _decodedData = [];
if (_receivedData != "") {
_decodedData = json.decode(_receivedData);
}
String _sentData = "";
if (_decodedData.contains(id)) {
_decodedData.remove(id);
_sentData = json.encode(_decodedData);
} else {
_decodedData.add(id);
_sentData = json.encode(_decodedData);
}
prefs.setString("favorites", _sentData);
_favoritos = _decodedData;
notifyListeners();
return _favoritos;}
The item I am trying to redraw is nested like this:
- MainPage: Scaffold body containing an IndexedStack
List<Widget> _widgetOptions = [
HomePage(),
FavoriteScreen(),
CompletedScreen()
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _selectedIndex,
children: _widgetOptions,
),
- FavoriteScreen: Statless widget only containing FavDoneList widget of my own.
- FavDoneList: Statefull widget that contains the list. -> I want to re-render it. I tried using a listener, but it does not redraw:
@override
Widget build(BuildContext context) {
var _favorites = Provider.of<FavoritesProvider>(context).favoritos;
if (flag == true) {
if (_favorites.isEmpty) {
return Center(child: Text("No has añadido favoritos"));
}
return ListView(
children: _loadFavorites(_favorites),
);
}
return Center(
child: CircularProgressIndicator(
color: Colors.red,
));
}
Can you help me understand what I am doing wrong? I can't figure out how to redraw it. Thanks!