Flutter: SchedulerBinding vs WidgetsBinding

Jitesh Mohite
FlutterWorld
Published in
2 min readOct 3, 2020

This is really a tricky one in which you don’t find any explanation around docs, but here I am going to share my experience which will help you to differentiate between them.

There are three callbacks available in flutter which rendering flutter widgets.

Transient callbacks: triggered by the system’s [Window.onBeginFrame] callback, for synchronizing the application’s behavior to the system’s display. For example, [Ticker]s and [AnimationController]s trigger from these.

Persistent callbacks: triggered by the system’s [Window.onDrawFrame] callback, for updating the system’s display after transient callbacks have executed. For example, the rendering layer uses this to drive its rendering pipeline.

Post-frame callbacks: which are run after persistent callbacks, just before returning from the [Window.onDrawFrame] callback. * Non-rendering tasks, to be run between frames. These are given a priority and are executed in priority order according to a schedulingStrategy.

All above callbacks run sequentially, but for us, the last callback would work i.e Post-frame callbacks

Widgets Binding: The glue between the widgets layer and the Flutter engine. which comes with WidgetsBindingObserver mixin which has many callbacks but one which required to use is didChangeAppLifecycleState that returns AppLifecycleState.

Scheduler Binding: This is also similar to WidgetBinding but it does not provide any life cycle callbacks.

The below binding example would be called exactly the ones, means print statement like WidgetsBinding & SchedulerBinding will be printed only once as we called it initState(), but it will be called when build method finished it’s rendering.

void initState() {
// TODO: implement initState
super
.initState();
print("initState");
WidgetsBinding.instance.addPostFrameCallback((_) {
print("WidgetsBinding");
});
SchedulerBinding.instance.addPostFrameCallback((_) {
print("SchedulerBinding");
});
}

addPostFrameCallback is run during a frame, just after the persistent frame callbacks (which is when the main rendering pipeline has been flushed). If a frame is in progress and post-frame callbacks haven’t been executed yet, then the registered callback is still executed during the frame. Otherwise, the registered callback is executed during the next frame.

The callbacks are executed in the order in which they have been added. Post-frame callbacks cannot be unregistered. They are called exactly once.

--

--

Jitesh Mohite
FlutterWorld

I am technology enthusiastic, want to learn things quickly and dive deep inside it. I always believe in developing logical things which makes impact on end user