Getx Controller not fetching data
you can pass data using getx argument https://www.kindacode.com/article/using-getx-get-for-navigation-and-routing-in-flutter/
Sharada Sharma
Updated on January 04, 2023Comments
-
Sharada Sharma over 1 year
So i have an implementation where i pass the id of a parent category and it fetches its child category from the database and displays them in another screen (view).
When i click on the parent, it sends the its category ID to the screen whose code is below and the it suppose to get the data from the database to be used in the screen. However, it says null when you click from the previous page to this new screen. If you directly do a hot reload, the data gets displayed.
Does it mean the below code doesn't get run when the screen is first loaded?
Note that all controllers and repositories have been set in the dependecies class and initialised in the main.
var childCatItem = Get.find<ChildCategoriesController>().getChildCategory(childCatId);
Parent Category Screen Function to Pass ID. The below code gets the ID from the already loaded data.
onTapLeftService: (){ int childCatId = randomController.isLoaded ? randomController.randomServicesList[0].children[0].id : 0; Get.toNamed(RouteHelper.getChildCategoryServicesPage(childCatId)); },
The below code is the route where it is gotten and passed to the screen
//This is the route implementation. This is called in the above code. static String getChildCategoryServicesPage(int childCatId) => '$childCategoryServicesPage?childCatId=$childCatId'; GetPage( name: childCategoryServicesPage, page: () { var childCatId = Get.parameters['childCatId']; return ChildCategoryServicesScreen( childCatId: int.parse(childCatId!)); }, transition: Transition.fadeIn),
import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:personal_start/controller/nonemergency_services/child_categories_controller.dart'; import 'package:personal_start/helper/app_styles.dart'; import 'package:personal_start/widget/title_text.dart'; class ChildCategoryServicesScreen extends StatelessWidget { int childCatId; //Here I receive the ID of the parent category into this screen ChildCategoryServicesScreen({Key? key, required this.childCatId}) : super(key: key); @override Widget build(BuildContext context) { var childCatInstance = Get.find<ChildCategoriesController>(); // I create an instance of the controller var childCatItem = Get.find<ChildCategoriesController>().getChildCategory(childCatId); //Here I make an API call to the database, passing the parent category ID. print(childCatItem); print(childCatInstance.childCategory!.title); //I receive a null value here. return Scaffold( appBar: AppBar( title: TitleTextWidget(titleText: 'Child Cat Title'), leading: GestureDetector( onTap: () { Get.back(); }, child: const Icon(Icons.arrow_back_outlined), ), backgroundColor: AppStyles.appPrimaryColor, ), // body: !childCatItem.isLoaded // ? const CustomLoader() // : Center(child: TitleTextWidget(titleText: 'It has Loaded')), ); } }
-
ShinMyth about 2 yearsKindly add the code on how you pass the parent category id from the previous page...
-
Sharada Sharma about 2 years@KeiCredo I have added the code snippets
-
-
Sharada Sharma about 2 yearsI already know how to pass the data. I have passed the data from a previous screen and using it in the current class which i have shared in code. It is simply not lading the controller data