code:
import 'dart:async';
void main() {
StreamController<int> controller = StreamController();
StreamSubscription streamSubscription =
controller.stream.listen((event) async {
await f(event);
});
controller.add(5);
controller.add(3);
controller.add(1);
}
Future<void> f(int duration) async {
await Future.delayed(Duration(seconds: duration));
print('$duration');
}
output: 1 3 5
the result i want: 5 3 1 How can I modify the code, or what other api to use
CodePudding user response:
Just remove your delay function and use the following:
import 'dart:async';
void main() {
StreamController<int> controller = StreamController();
StreamSubscription streamSubscription =
controller.stream.listen(
(event) => print('Event: $event'),
onDone: () => print('Done'),
one rror: (error) => print(error),
);
controller.add(5);
controller.add(3);
controller.add(1);
}
CodePudding user response:
Don't use streams?
Why are you using streams if you want to ruin the point of using them.
void main() async {
await f(5);
await f(3);
await f(1);
}
Future<void> f(int duration) async {
await Future.delayed(Duration(seconds: duration));
print('$duration');
}
output: 5 3 1 with delay 5s, 3s, 1s as you wanted