Skip to content

Instantly share code, notes, and snippets.

@dj-kusuha
Last active July 4, 2025 16:06
Show Gist options
  • Save dj-kusuha/93acea4fe2b26a147992dd4c3b19b30d to your computer and use it in GitHub Desktop.
Save dj-kusuha/93acea4fe2b26a147992dd4c3b19b30d to your computer and use it in GitHub Desktop.
[Flutter / Dart] 非同期処理を終える前に再度処理を実行しようとしたら無視する hook 実装
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