gpt4 book ai didi

flutter - 在flutter中使用whatsapp包时如何按顺序发送消息

转载 作者:行者123 更新时间:2023-12-03 03:33:04 27 4
gpt4 key购买 nike

我有一个需要发送给不同人的消息列表。在预览页面中,列表是有序的。但是当我按下发送时,它会一一进入WhatsApp聊天以随机发送消息。如何按照预览消息中显示的顺序进行制作?有人可以帮我弄这个吗?
我在这里调用预览页面:

 if (messages.isNotEmpty) {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => PreviewPage(messages)));
}
这是预览页面
import 'package:android_intent/android_intent.dart';
import 'package:flutter/material.dart';

class PreviewPage extends StatelessWidget {
final List<Map> msgList;
final String baseURL = "https://api.whatsapp.com/send?phone=";
PreviewPage(this.msgList);

@override

widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Preview Message"),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
//msgList = SortedMap(Ordering.byValue());
// print(msgList["phone"]);
for (var msg in msgList) {
print('printing message');
print(msg["phone"]);
if (msg["phone"] != null) {
var url = "${baseURL}91${msg['phone']}&text=${msg['messages']}";
print(url);
AndroidIntent intent = AndroidIntent(
action: 'action_view',
data: Uri.encodeFull(url),
// flags: <int>[Flag.FLAG_ACTIVITY_CLEAR_TOP],
package: "com.whatsapp.w4b");
await intent.launch();
}
}
},
child: Icon(Icons.send),
),

最佳答案

尝试将 OnPressed 更改为 async 并引用 android-intent package 使用 await intent.launch();

onPressed: () async {
//msgList = SortedMap(Ordering.byValue());
// print(msgList["phone"]);
for (var msg in msgList) {
print('printing message');
print(msg["phone"]);
if (msg["phone"] != null) {
var url = "${baseURL}91${msg['phone']}&text=${msg['messages']}";
print(url);
AndroidIntent intent = AndroidIntent(
action: 'action_view',
data: Uri.encodeFull(url),
// flags: <int>[Flag.FLAG_ACTIVITY_CLEAR_TOP],
package: "com.whatsapp.w4b");
await intent.launch();
}
}
},
下面给出的代码用于排序。创建了一个用于组织消息详细信息的类,例如联系电话、联系人姓名、消息内容。这有助于稍后根据联系人姓名应用 sort 函数。如果需要,根据其他属性修改 sort
List.sort 的引用
class MessageHolder {
int contactNumber;
String messageContent;
String contactName;
MessageHolder({this.contactName, this.contactNumber, this.messageContent});

@override
String toString(){
return '$contactName, $contactNumber, $messageContent';
}
}

void main() {
List<MessageHolder> messages = [
MessageHolder(
contactName: 'DEF', contactNumber: 2345678901, messageContent: 'Two'),
MessageHolder(
contactName: 'ABC', contactNumber: 1234567890, messageContent: 'One'),

MessageHolder(
contactName: 'GHI', contactNumber: 3456789012, messageContent: 'Three'),
MessageHolder(
contactName: 'XYZ', contactNumber: 3456789012, messageContent: 'Four'),
];

List<MessageHolder> sortedMessages = messages..sort((a, b) => a.contactName.compareTo(b.contactName));

for(MessageHolder message in sortedMessages){
print(message);
}
}
输出:
ABC, 1234567890, One
DEF, 2345678901, Two
GHI, 3456789012, Three
XYZ, 3456789012, Four

关于flutter - 在flutter中使用whatsapp包时如何按顺序发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62744494/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com