일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- .dio
- Flutter
- flutter doctor -v
- appstore connect guideline
- app stroe connect guideline
- information needed
- exception
- youtube_player_flutter
- AI
- providernotfoundexception
- infinity or nan toint
- app store connect guideline
- 채팅 메시지 정렬
- flutter_secure_storage
- permissiondeniedexception
- pub.dev
- Guideline 5.1.1
- undefined name
- 에러
- flutter_dotenv
- dart sdk version upgrade
- buildcontext
- withopacity
- appstroe connect guideline
- guideline 1.5
- 플러터
- app completeness
- tflite_flutter
- exception caught by image resource service
- guideline 4.3(a)
Archives
- Today
- Total
min_chan님의 블로그
[Flutter] - API 호출 최적화 Throttle 본문
Throttle을 사용한 이유
- 버튼과 같은 이벤트를 통해 사용자와 서버 간의 빈번한 데이터 통신은 많이 발생한다.
- 만약 동일한 이벤트에서 과도한 API 호출이 발생하는 경우, 불필요한 네트워크 트래픽 증가와 서버 과부하를 초래할 수 있다.
- Throttle 기법은 특정 시간 간격 동안 한 번의 작업(예: API 호출)만 실행되도록 제한하는 기법으로 동일한 작업이 연속적으로 호출되지 않도록 하여 서버와 네트워크의 부하를 줄이기 위해 사용하였다.
Throttle
class Throttle {
bool _isThrottled = false;
void call(Future<void> Function() action, Duration duration) async {
if (_isThrottled) return;
_isThrottled = true;
await action();
Future.delayed(duration, () {
_isThrottled = false;
});
}
}
final throttle = Throttle();
Button
GestureDetector(
onTap: () {
throttle.call(() async {
if (!isLike) {
// 좋아요 api 호출
} else {
// 좋아요 해제 api 호출
}
}, Duration(seconds: 1));
},
child: FaIcon(
isLike == false
? FontAwesomeIcons.heart
: FontAwesomeIcons.solidHeart,
size: MediaQuery.of(context).size.width > 600 ? 35 : 25,
color: Colors.red,
),
);
'Flutter' 카테고리의 다른 글
[Flutter] - Unknown flutter tag. Abandoning upgrade to avoid destroying local changes. (0) | 2025.01.23 |
---|---|
[Flutter] - Unable to find bundled Java version (2) | 2025.01.22 |
[Flutter] - Vertical viewport was given unbounded height (0) | 2025.01.20 |
[Flutter] - Don't use 'BuildContext's across async gaps, guarded by an unrelated 'mounted' check. (0) | 2025.01.17 |
[Flutter] - pub get failed (0) | 2025.01.16 |