Closed skyturkish closed 2 years ago
we already take any change about userInformation we should just listen to user information, this is the solution.
class FavoriteIconButton extends StatefulWidget {
const FavoriteIconButton({
super.key,
required this.product,
required this.provider,
});
final Product product;
final UserInformationNotifier provider;
@override
State<FavoriteIconButton> createState() => _FavoriteIconButtonState();
}
class _FavoriteIconButtonState extends State<FavoriteIconButton> {
@override
Widget build(BuildContext context) {
final isFavorite =
context.watch<UserInformationNotifier>().userInformation.favoriteAds.contains(widget.product.productId);
return IconButton(
onPressed: () {
isFavorite
? widget.provider.removeFavoriteProduct(productId: widget.product.productId)
: widget.provider.addFavoriteProduct(productId: widget.product.productId);
setState(() {});
},
icon: Icon(
Icons.favorite,
color: isFavorite ? Colors.red : Colors.black,
),
);
}
}
We can only stream isFavorite or not ? and wrap StreamBuilder Iconbutton,