react에서 쓰던 방식이랑 조금은 달라서 적어봅니다.
[websocket] 데이터갱신 -> rqc add (subscribe)
rqc get -> view
초기 [api] 데이터 + rqc update
import 'dart:async';
// subscribe
class StreamBroadcast<T> {
late final StreamController<T> streamController =
StreamController.broadcast();
late final Stream<T> stream = streamController.stream;
StreamSubscription<T> get(
void Function(T event) pCallbackFuntion, isValidate) {
// isValidate 체크 로직 넘어오면 맞는 것만 함수 실행
if (isValidate != null) {
return stream.listen((data) {
if (isValidate(data)) pCallbackFuntion(data);
});
}
// 다 실행
return stream.listen(pCallbackFuntion);
}
void add(T data) {
streamController.sink.add(data);
}
void close() {
streamController.close();
}
}
StreamBroadcast()로 할당을 하고,
소켓 데이터를 add로 넣고, get으로 화면단에서 연결해두면 실시간으로 들어오게된다.
이게 close를 할때는 소켓이 필요없어졌을대나 이럴때 써야 할것 같은대 현재로서는 사용할 시점이 없다..
먼가 찜찜하지만.. 흐음..
'www > flutter' 카테고리의 다른 글
[flutter] ( flutter || React Native ) + Kotlin/Swift => app (0) | 2023.09.12 |
---|---|
flutter 정리 (0) | 2023.07.27 |
[flutter] mac Android Studio (version 2022.1) ✗ Unable to find bundled Java version. (0) | 2023.07.10 |
[flutter] CocoaPods not installed. (0) | 2023.07.09 |
[flutter] doctor --android-licenses (0) | 2023.07.09 |