I have a list of news that comes from the API on the screen. Output code for one card with news(I shortened the code and left only the necessary):
Widget customListTile(Article article, BuildContext context) {
final newsController = Get.put(FavoritesController());
return InkWell(
Container(
child:Row(
textDirection: TextDirection.ltr,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
article.source.name,
style: TextStyle(
color: Colors.white,
backgroundColor: Colors.red,
),
),
IconButton(
onPressed: () {
newsController.addNews(article);
},
icon: const Icon(Icons.favorite_border)),
]
)
),
);
}
There is an icon, when clicked, the addNews() function is called.And there is a controller code where there is this function:
class FavoritesController extends GetxController {
var _news = {}.obs;
void addNews(Article article) {
if(_news.containsKey(article)) {
}
}
get news => _news;
}
I need when clicking on the icon, the news was added to a separate Saved page. How to write a function for adding news in a controller? And did I write the code correctly? Can you please tell me how this can be implemented?