Last active
July 4, 2025 16:06
-
-
Save dj-kusuha/93acea4fe2b26a147992dd4c3b19b30d to your computer and use it in GitHub Desktop.
[Flutter / Dart] 非同期処理を終える前に再度処理を実行しようとしたら無視する hook 実装
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:flutter_hooks/flutter_hooks.dart'; | |
typedef Callback = FutureOr<void> Function(); | |
class AsyncGuard extends ChangeNotifier { | |
bool _isRunning = false; | |
bool get isRunning => _isRunning; | |
Future<void> run(Callback callback) async { | |
if (_isRunning) return; | |
_isRunning = true; | |
notifyListeners(); | |
try { | |
await callback(); | |
} finally { | |
_isRunning = false; | |
notifyListeners(); | |
} | |
} | |
} | |
AsyncGuard useAsyncGuard() { | |
final guard = useMemoized(() => AsyncGuard()); | |
useListenable(guard); | |
return guard; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment