Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call async functions in Stream.periodic

Tags:

flutter

dart

Is it possible to call an async function inside dart:Stream.periodic function?

I tried to wrap my async function but it is not working, please see code below.

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

String _checkConnectivity(int x) async {

return await _connectionRepository.checkConnection();

}
like image 642
Cedric Sarigumba Avatar asked Aug 19 '19 15:08

Cedric Sarigumba


1 Answers

Use asyncMap:

  Stream<String> checkConnectionStream() async* {
    yield* Stream.periodic(Duration(seconds: _pollingInterval), (_) {
      return _connectionRepository.checkConnection();
    }).asyncMap((event) async => await event);
  }
like image 58
MDemetrio Avatar answered Sep 18 '22 02:09

MDemetrio