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, 2023 in Android by sarit
• 1,830 points
887 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.

To know more, join our Flutter Training today.

answered Mar 20, 2023 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, 2023 in Android by vinayak
519 views
0 votes
1 answer

flutter remove back button on appbar

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

answered Mar 10, 2023 in Android by vinayak
7,705 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, 2023 in Android by vishalini
1,790 views
0 votes
1 answer

Waiting asynchronously for Navigator.push() - linter warning appears: use_build_context_synchronously

The "use_build_context_synchronously" lint warning is raised when ...READ MORE

answered Apr 13, 2023 in Flutter by Tej
2,047 views
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
3,220 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
788 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,839 views
0 votes
2 answers

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

how to change the app icon in ...READ MORE

answered Nov 29, 2023 in Android by anonymous
9,491 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, 2023 in Android by vishalini
776 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