You should have a list that contains favoriteStatus.
//length should be equal serviceProvider.justPostsModel.length
//put some initial value (true, false) in each index according to its favorite status
List<bool> isFavourite;
then
ListView.builder(
itemCount: serviceProvider.justPostsModel.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(left: 4.0, right: 4.0, bottom: 4),
child: InkWell(
onTap: () {
},
child: Container(
width: MediaQuery.of(context).size.width,
child: Card(
child: Column(
children: [
Padding(
padding:
const EdgeInsets.only(left: 8.0, right: 12),
child: Row(
children: [
InkWell(
onTap: () async {
if (isFavourite[index] == false) {
await serviceProvider.setToggleLike(
context,
serviceProvider
.justPostsModel[index].id);
await serviceProvider
.getPostsList(context);
setState(() {
isFavourite[index] = true;
});
} else {
await serviceProvider.setToggleLike(
context,
serviceProvider
.justPostsModel[index].id);
await serviceProvider
.getPostsList(context);
setState(() {
isFavourite[index] = false;
});
}
},
child: Icon(
isFavourite[index] == false
? Icons.favorite_border
: Icons.favorite,
color: Constants.skyColor(),
),
),
],
),
),
]),
),
),
),
);
},
),
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…