Flutter run async function on stream listen

0 votes

I have a stream of data that gets me messages, and on each message, I want to execute an HTTP request, i.e. an async function. How can I do that?

Minimum reproducible example:

Future<int> myFuture(int n) async {
  Future.delayed(Duration(seconds: 2));  // Mock future
  print("Future finished for: $n");
  return n;
}

Stream<int> countStream(int to) async* {
  for (int i = 1; i <= to; i++) {
    yield i;
  }
}

void main() async {
  var stream = countStream(10);
  stream.listen((n) { await myFuture(n); });
  // or simply: stream.listen(myFuture);
}

Essentially I can't await myFuture.

Mar 16 in Android by sarit
• 1,430 points
219 views

1 answer to this question.

0 votes

You can make the listen callback an async function, and then await the call to myFuture. Here's an updated version of your example code:

Future<int> myFuture(int n) async {
  await Future.delayed(Duration(seconds: 2));  // Mock future
  print("Future finished for: $n");
  return n;
}

Stream<int> countStream(int to) async* {
  for (int i = 1; i <= to; i++) {
    yield i;
  }
}

void main() async {
  var stream = countStream(10);
  stream.listen((n) async {
    await myFuture(n);
  });
}

Alternatively, you could use the asyncMap method on the Stream object to apply the asynchronous operation to each element of the stream:

void main() async {
  var stream = countStream(10);
  await for (var result in stream.asyncMap((n) => myFuture(n))) {
    // do something with the result
  }
}

In this example, asyncMap applies the myFuture function to each element of the stream, and returns a new stream containing the results. The await for loop then iterates over the results of the new stream.

answered Mar 20 by pooja

Related Questions In Android

0 votes
1 answer
0 votes
1 answer

How to run code after some delay in Flutter?

You can use the Future.delayed method to ...READ MORE

answered Mar 10 in Android by vinayak
163 views
0 votes
1 answer

flutter remove back button on appbar

You can remove the back button from ...READ MORE

answered Mar 10 in Android by vinayak
872 views
0 votes
1 answer

How to run code after some delay in Flutter?

You can use the Future.delayed function in ...READ MORE

answered Mar 18 in Android by vishalini
376 views
0 votes
1 answer
0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

Hi@akhtar, You need to add the main() function ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,440 points
2,758 views
0 votes
1 answer

How to install Dart in Windows system?

Hi@akhtar, Dart is the programming language used to code Flutter apps. ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,440 points
570 views
0 votes
1 answer

flutter run: No connected devices

Hi@akhtar, To prepare to run and test your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,440 points
3,431 views
0 votes
1 answer

How to change the application launcher icon on Flutter? Ask Question

Yes, you can change the launcher icon ...READ MORE

answered Mar 1 in Android by vishalini
1,439 views
0 votes
1 answer

Flutter - Wrap text on overflow, like insert ellipsis or fade

Container( padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 18.0), ...READ MORE

answered Mar 1 in Android by vishalini
295 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP