FrameThrottle class
A single-slot gate for camera-frame processing that drops frames arriving while a previous frame is still being processed.
Live camera streams deliver frames faster than on-device inference can keep
up. Running every frame would queue work and add latency without improving
the result, so the usual pattern is to process a frame only once the
previous one has finished and drop the rest. FrameThrottle captures that
pattern so callers do not have to hand-roll a mutable "busy" flag and the
try/finally that must always clear it (forgetting the finally wedges
the gate closed forever).
The busy check and set happen synchronously before the first await, so on
Dart's single-threaded event loop there is no race between overlapping
stream callbacks.
Usage inside a camera image-stream callback:
final _throttle = FrameThrottle();
void _onCameraImage(CameraImage image) {
_throttle.run(() async {
final results = await detector.detectFromCameraImage(image);
if (mounted) setState(() => _results = results);
});
}
Constructors
Properties
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reset(
) → void - Clears the busy flag without running anything.
-
run<
T> (Future< T> task()) → Future<T?> -
Runs
taskunless another task is already in flight. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited