I am working on a Flutter project using GetX for state management, and we have a booking module that includes listing, creation, editing, updating, and deleting features.
For this module, we have three separate GetXController files:
CreateBookingController
EditBookingController
UpdateBookingController
In the listing screen, there is an edit option, where we need to initialize both the EditBookingController and UpdateBookingController. However, when these controllers are initialized in the listing screen, their onInit() methods are called prematurely.
The issue arises when navigating to the edit screen—since the controller is already instantiated, its onInit() does not trigger again. The same problem occurs with the update functionality.
Challenges Faced:
onInit()is called too early when the controller is initialized in the listing screen.When navigating to the edit screen,
onInit()does not re-trigger as expected.The update functionality faces the same issue.
Question:
How can I manage the lifecycle of these controllers effectively so that onInit() is called at the right time when navigating between screens? What is the best approach to handling this scenario in GetX?
I will put() the edit and update controllers again on both of the screens but it reduces the app performance, because api's are called two times. But I need to call at once.
How can I do this?
Listing screenCode
class MetaFourOutBoundList extends GetView<MetaFourParcelController> {
MetaFourOutBoundList({super.key});
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
final metaFourParcelController = Get.put(MetaFourParcelController());
final metaFourEditParcelController = Get.put(MetaFourEditParcelController());
MetaFourWidgets metaFourWidgets = MetaFourWidgets();
final scrollController = ScrollController();
SizedBoxWidget sizedBoxWidget = SizedBoxWidget();
TextWidgets textWidgets = TextWidgets();
@override
Widget build(BuildContext context) {
if (!metaFourParcelController.isInitializedForOutBound) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
scrollController.addListener(() async {
if (scrollController.position.pixels == scrollController.position.maxScrollExtent) {
// Load more data when reaching the end
if (controller.pageNumber < controller.lastPage) {
controller.pageNumber++;
await controller.getBookingsList();
metaFourParcelController.isInitializedForOutBound = true; // Set the flag to true after initialization
controller.update();
}
}
});
});
}
return GetBuilder(
init: metaFourParcelController,
builder: (controller) {})}
class MetaFourEditParcel extends GetView<MetaFourEditParcelController> {
MetaFourEditParcel({super.key});
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
final scrollController = ScrollController();
final globalController = Get.put(GlobalController());
final metaFourEditParcelController = Get.put(MetaFourEditParcelController());
MetaFourWidgets metaFourWidgets = MetaFourWidgets();
SizedBoxWidget sizedBoxWidget = SizedBoxWidget();
TextWidgets textWidgets = TextWidgets();
final metaFourParcelController = Get.put(MetaFourParcelController());
@override
Widget build(BuildContext context) {
if (!metaFourEditParcelController.isInitializedForEditParcel) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
metaFourEditParcelController.hostName = globalController.userName;
metaFourEditParcelController.hostDesignation =
globalController.designation;
metaFourEditParcelController.hostPhone = globalController.profilePhone;
metaFourEditParcelController.hostEmail = globalController.email;
metaFourEditParcelController.hostImage =
globalController.profilePicture;
metaFourEditParcelController.costController.text =
globalController.profileCostCenter;
metaFourEditParcelController.searchProfileEditController.clear();
metaFourEditParcelController.showEditList = true;
metaFourEditParcelController.editFilteredNames = [];
metaFourEditParcelController.showEditSearchBox = false;
metaFourEditParcelController.isInitializedForEditParcel = true;
metaFourEditParcelController
.getselectedCountriesAPIFn(); // get selected countries List
if (controller.selectedCountriesNameList.any(
(element) =>
element.key?.toUpperCase() ==
controller.senderCountryEditCode.toUpperCase(),
)) {
// call api for getting labels of the changabkle feilds.
controller.getLabelsAPI(
code: controller.senderCountryEditCode.toUpperCase(),
getFor: "sender");
log("country is matched");
}
metaFourEditParcelController.update();
});
}
return GetBuilder(
init: metaFourEditParcelController,
builder: (controller) {})}