Call api after bage build in getx

Issue

Im using getx and i want to call api after bage build, because this api its not necessary part of the page build.. What i know is this way

@override
void initState(){
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback((_){
   
  });
  
}

But what about getx best way ?

Solution

the Getx package offers lifecycle methods that are very convenient in you’re cases, what you need here is to use the onReady(), which is called one frame after the onInit() is executed, you can say that its equivalent to the:

   @override
    void initState(){
      super.initState();
      WidgetsBinding.instance.addPostFrameCallback((_){  
    // Here is the code which will be executed exactly after initState using StatefulWidget
      });          
    }

you can use onReady() like this:

 class TextController extends GetxController {
 @override
  void onInit() {
   /*...*/
  }
  void onReady() {
   // Here is the code which will be executed exactly after onInit using Getx
  }
}

Answered By – Gwhyyy

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *