是否可以在 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/