flutter - 扑。未来应该等待回调
问题描述
我正在 Flutter 中实现华为推送通知。这里:https ://pub.dev/packages/huawei_push/example
我希望能够有一种调用方式:
String? token = await PushNotificationsService().getPushNotificationsToken();
从我的代码中,让我自己的类PushNotificationService()
获取令牌并返回它。
我的问题是,对于华为,我需要在PushNotificationsService()
课堂上实现一些监听器。
void _onTokenEvent(String event) {
_token = event;
showResult("TokenEvent", _token);
}
void _onTokenError(Object error) {
PlatformException e = error as PlatformException;
showResult("TokenErrorEvent", e.message!);
}
...
Push.getTokenStream.listen(_onTokenEvent, onError: _onTokenError);
但是我应该如何设置我的getPushNotificationsToken()
?我需要在其中返回一个未来,但是如何处理它Future
以便只有在我的两个侦听器回调之一被调用时才“触发”它?!
我确信这是一个基本的解决方案,但不幸的是我是 Flutter 的新手。
解决方案
目前Flutter Push Kit插件只能通过事件监听获取token信息。
推荐阅读
- android - 为什么之前在 Android 11 上运行时,Android 文件创建突然失败?
- r - 使用 R tidyr pivot_wide 从多个列名和值中获取宽格式数据
- python - 网页抓取网站时无法获取文本
- android - Android如何在点击expandablelistview时获取孩子的父母姓名
- python - 有什么方法可以调整 DAG 应该有多“胖”和多“高”?
- c++ - 返回完整或空 std::optional 时的 if-then-else 与三元运算符
- visual-studio-code - 当另一个调试会话关闭时关闭一个调试会话
- android - 如何使用电子邮件和密码生成 Youtube 的 API Key
- r - 如果索引从 1 开始,为什么矩阵允许索引为零?
- c++ - 多项式不可约性的错误评估