Home > front end >  Post Frame Callback - Looping
Post Frame Callback - Looping

Time:11-17

I have been checking performance on my app and noticed that one of the widget constantly loops. in that Widget I am using the following code to retrieve data from Firestore DB, however for this example I have simplified it with the same looping result.

Question: Is there a reason why Widget Binding is called so many times and in a loop? I was under the impression it was called once on widget build complete. Should I be using something else for a one off post build function?

I have 7 of these widgets in a listView, so I should she maximum 7 I believe.

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPersistentFrameCallback((timeStamp) async {
    print(' Is callback done?');
      if (mounted) {
        setState(() {
          isLoaded = true;
        });
      }
    });
  }

The widget itself is very simple with just a Text Widget

  @override
  Widget build(BuildContext context) {
    return Text('Hello');
   );
  }

In my logs I see the following which just keeps going up and up and up.

enter image description here

CodePudding user response:

Maybe you want addPostFrameCallback instead of addPersistentFrameCallback ?

As per documentation addPersistentFrameCallback:

Once registered, they are called for every frame for the lifetime of the application.

As per documentation addPostFrameCallback:

Post-frame callbacks cannot be unregistered. They are called exactly once.

  • Related