flutter - 如何在 Stream.periodic 中调用异步函数

标签 flutter dart

是否可以在 dart:Stream.periodic 函数中调用异步函数?

我试图包装我的异步函数,但它不起作用,请参阅下面的代码。

Stream.periodic(Duration(seconds: _pollingInterval), _checkConnectivity)

String _checkConnectivity(int x) async {

return await _connectionRepository.checkConnection();

}

最佳答案

使用异步映射:

  Stream<String> checkConnectionStream() async* {
    yield* Stream.periodic(Duration(seconds: _pollingInterval), (_) {
      return await _connectionRepository.checkConnection();
    }).asyncMap((event) async => await event);
  }

关于flutter - 如何在 Stream.periodic 中调用异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57559823/

相关文章:

firebase - 从 Flutter 应用程序在文档中创建子集合

android - 带代理的 Flutter WebView

flutter - 如何声明字节列表?

static - 在 Flutter 中保留所有常量的最佳做法是什么?

android - 输入几行后文本输入的底部溢出 flutter

android - 如何在statelessWidget类中使用setState()函数

Flutter Provider - ChangeNotifierProxyProvider 设置依赖

html - 如何显示 Dart :html file image in Flutter web?

intellij-idea - XMLHttpRequest使用IntelliJ在Dart中进行跨源请求

dart - 我可以多次监听 StreamController 的流(但一次不能订阅多个)吗?