I am trying to target a particular index to perform (send/reject/confirm) functions but unfortunately when I press a single button function applies to all buttons. What should I do to solve this issue? Please help me. I have been stuck for days.When I click on single button All have changed
I made a model and fetched a unique id but all logic made me stuck, can anyone describe this logic in a simple way? I am providing the main code where I call the function.
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
backgroundColor: mainColor,
title: Text("Choose Your Partner")),
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: StreamBuilder(
stream: FirebaseFirestore.instance.collection('recentPublished') .snapshots(),
builder: (context, snapshot) {
userId = FirebaseAuth.instance.currentUser.uid;
ListOfIndex list = ListOfIndex(snapshot);
if (!snapshot.hasData) {
return Center(
child: const CupertinoActivityIndicator());
} else {
return ListView.builder(
shrinkWrap: true,
primary: false,
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index) {
return Column(
children: [
_vehicleCard(
userId,
snapshot.data.docs[index]["userId"]
.toString(),
snapshot.data.docs[index]["riderProfileUrl"]
.toString(),
userName,
snapshot.data.docs[index]["rideOfferBy"]
.toString(),
snapshot.data.docs[index]["travellingDate"]
.toString(),
snapshot.data.docs[index]["pickUpPlace"]
.toString(),
snapshot.data.docs[index]["destination"]
.toString(),
snapshot.data.docs[index]["vehicleNumber"]
.toString(),
snapshot
.data.docs[index]["vehicleName&Models"]
.toString(),
snapshot.data.docs[index]["vehicleColor"]
.toString(),
snapshot.data.docs[index]["seat"]
.toString(),
snapshot.data.docs[index]["fairPrice"]
.toString(),
snapshot.data.docs[index]["fcmToken"]
.toString()),
(pending == false) ?
ElevatedButton(
onPressed: () async {
if(snapshot.data.docs[index]["fcmToken"] == "upcoming")
{
pending = true;
setState(() {
});
}
try {
final body = {
"to": snapshot.data.docs[index]["fcmToken"]
.toString(),
"notification": {
"title": snapshot.data.docs[index]["rideOfferBy"]
.toString(),
"body":
"Are you ready for this ride?",
"android_channel_id":
"Vision DG Tech"
},
'data': {'type': 'notify',
'status': 'pending'
}
};
var res = await post(
Uri.parse(
'https://fcm.googleapis.com/fcm/send'),
body: jsonEncode(body),
headers: {
HttpHeaders.contentTypeHeader:
'application/json; charset=UTF-8',
HttpHeaders
.authorizationHeader:
"key=$key"
},
);
pending = true;
setState(() {});
print('Res -------- ${res.body}');
} catch (_) {}
},
child: Text(
(pending == false)?"Send Request":"Pending...",
),
style: ButtonStyle(
backgroundColor: MaterialStatePropertyAll(
pSecondaryColor)),
) : TextButton(onPressed: (){}, child: Text("Pending..."))
],
);
},
);
}
}),
)
],
),
),
),
);
You are using a single variable for many list items.
You have to target each list item individually.
For that, you have to create a list where, for each item
pendingisfalse.try this code