As you all can see below code contains a button that on clicked sends the data from Cloud-Firestore as a Message on WhatsApp using the package 'whatsapp_unilink' and 'url_launcher'/
String? product;
String? qnty;
Cbutton(
text: 'Whatsapp',
onPressed: () async {
snapshot.data.docs
.forEach((value) async {
product = value.data()['Product'];
qnty = _controller.text;
print(product);
final link = WhatsAppUnilink(
phoneNumber: ' 914534534553',
text: '- $product = $qnty \n',
);
await launch('$link');
print('$link');
});
},
),
above code returns this
I/flutter (20941): ASHNIL SYRUP (200ML)
I/flutter (20941): OLSEPT PLUS MW GARGLE
I/flutter (20941): https://wa.me/912342424344?text=- ASHNIL SYRUP (200ML) = 3
I/flutter (20941): https://wa.me/912423423444?text=- OLSEPT PLUS MW GARGLE = 3
meaning it generates a message(Link) for each of the data separately which is not my goal, the goal is to have all the data in a single message(Link) and then send it to the receiver, like this:
https://wa.me/912342424344?text=- ASHNIL SYRUP (200ML) = 3 - OLSEPT PLUS MW GARGLE = 3
So how can I do this?
Note that the issue has nothing related to the Phone Number, it's just a dummy number!
CodePudding user response:
Create an empty string. Add values to in in loop then on completion send the message.
Cbutton(
text: 'Whatsapp',
onPressed: () async {
String message = "";
snapshot.data.docs
.forEach((value) async {
product = value.data()['Product'];
qnty = _controller.text;
print(product);
message ='- $product = $qnty \n';
});
final link = WhatsAppUnilink(
phoneNumber: ' 914534534553',
text: message
);
await launch('$link');
},
),