검색해보면 여러가지 방법이 있는데 그중 stream 사용하는게 제일 편해서 아래와 같이 사용합니다. 주로 Getx을 사용하다 보니 mixin 형태로 만들어서 사용하고 있으며 아래 코드 보면서 필요한 위치에 코드를 추가하면 됩니다.
mixin SequentialRunMixin on GetxController {
final orderedFuturesController = StreamController();
late final StreamSubscription orderedFuturesSubscription;
@override
void onInit() {
super.onInit();
orderedFuturesSubscription = orderedFuturesController.stream
.asyncMap((future) async => await future())
.listen((_) {}, cancelOnError: false, onError: (e, s) {
logger.e(e);
});
}
@override
void onClose() async {
// await orderedFuturesController.stream.isEmpty;
await orderedFuturesSubscription.cancel();
super.onClose();
}
}
호출은 아래와 같이 async function으로 한번 더 감싸줍니다. 그렇지 않으면 add하는 순간 함수가 실행되어 순차적으로 실행되지 않습니다.
orderedFuturesController.add(() async {
await sequential(3);
});
orderedFuturesController.add(() async {
await sequential(1);
});
아래와 같이 순차적으로 잘 실행됩니다.
I/flutter (22321): ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (22321): │ #0 DevService.sequential (package:shim/dev/dev.service.dart:55:12)
I/flutter (22321): │ #1 DevService.sequentialTest.<anonymous closure> (package:shim/dev/dev.service.dart:37:13)
I/flutter (22321): │ #2 SequentialRunServiceMixin.onInit.<anonymous closure> (package:shim/mixin/sequential.dart:39:49)
I/flutter (22321): ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
I/flutter (22321): │ 💡 start: 3
I/flutter (22321): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (22321): ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (22321): │ #0 DevService.sequential (package:shim/dev/dev.service.dart:61:12)
I/flutter (22321): │ #1 <asynchronous suspension>
I/flutter (22321): │ #2 DevService.sequentialTest.<anonymous closure> (package:shim/dev/dev.service.dart:37:7)
I/flutter (22321): ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
I/flutter (22321): │ 💡 end: 3
I/flutter (22321): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (22321): ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (22321): │ #0 DevService.sequential (package:shim/dev/dev.service.dart:55:12)
I/flutter (22321): │ #1 DevService.sequentialTest.<anonymous closure> (package:shim/dev/dev.service.dart:40:13)
I/flutter (22321): │ #2 SequentialRunServiceMixin.onInit.<anonymous closure> (package:shim/mixin/sequential.dart:39:49)
I/flutter (22321): ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
I/flutter (22321): │ 💡 start: 1
I/flutter (22321): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (22321): ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (22321): │ #0 DevService.sequential (package:shim/dev/dev.service.dart:61:12)
I/flutter (22321): │ #1 <asynchronous suspension>
I/flutter (22321): │ #2 DevService.sequentialTest.<anonymous closure> (package:shim/dev/dev.service.dart:40:7)
I/flutter (22321): ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
I/flutter (22321): │ 💡 end: 1
I/flutter (22321): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
'개발 > Flutter' 카테고리의 다른 글
Pageview swipe gesture 감지하기 (0) | 2025.02.12 |
---|---|
안드로이드 스튜디오에서 invalid java_home 에러 처리 (0) | 2023.03.23 |
xcode 13.4.1에서 iOS 빌드시 버그 해결하기 (0) | 2022.09.07 |
xcode 14 베타 사용시 Invalid Bundle로 배포 안되는 버그 수정 (0) | 2022.09.05 |
캐러셀 리스트뷰 만들기 (0) | 2018.12.21 |
배경이 투명한 페이지 만들기 (0) | 2018.12.06 |