Home > Net >  Getx Controller not fetching data
Getx Controller not fetching data

Time:04-25

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);
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')),
    );
  }
}

CodePudding user response:

you can pass data using getx argument https://www.kindacode.com/article/using-getx-get-for-navigation-and-routing-in-flutter/

CodePudding user response:

Try this way

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 GetView<ChildCategoriesController> {
ChildCategoryServicesScreen({Key? key}): super(key: key);

@override
ChildCategoriesController controller = Get.put(ChildCategoriesController());



@override
  Widget build(BuildContext context) {
    ...//TODO: YOUR CODE HERE

  }

ChildCategoriesController
class ChildCategoriesController extends GetxController {
late int childCatId;
...//TODO: YOUR CODE HERE

  @override
  void onInit() {
    int childCatId = Get.arguments[0];
    ...//TODO: YOUR CODE HERE
    super.onInit();
  }

  @override
  void onClose() {}

...//TODO: YOUR CODE HERE
}
  • Related