5

How can I call a specific function in my stateless widget, whenever my GetX state changes? E.g. whenever the index of my bottomnavigationbar changes, how can I trigger an event?

    Obx(
        () =>
           BottomNavigationBar(
               items: const <BottomNavigationBarItem>[
                  BottomNavigationBarItem(
                   icon: Icon(Icons.explore),
                   label: 'Erkunden',
                  ),           
               ],
               currentIndex: bottomNavigationController.index.value,
               onTap: (index) {
                     bottomNavigationController.changeIndex(index);
               },
            )
     ),

EDIT:

 class BottomNavigationController extends GetxController {
     RxInt index = 0.obs;

     void changeIndex(int val) {
          index.value = val;
     }
 }

4 Answers 4

11

Get your controller, listen changes in a method according to your requirements. It may be in constructor, build method, or a button's onPress method etc.

BottomNavigationController bnc = Get.find();

bnc.index.listen((val) {
  // Call a function according to value
});

Sign up to request clarification or add additional context in comments.

3 Comments

Where do I put this listener?
You can put it in costructor, build method etc.
You can put in controller onReady or onInit methods
6

To expand on @ertgrull's answer:

You can add a listener to any stream, but Getx also has built in worker functions for this type of functionality.

Usually when I need this I add the listener in the onInit of the controller.

class BottomNavigationController extends GetxController {
  RxInt index = 0.obs;

  void changeIndex(int val) {
    index.value = val;
  }

  @override
  void onInit() {
    super.onInit();

    ever(index, (value) {
  // call your function here
  // any code in here will be called any time index changes
    });
  }
}

5 Comments

I heard about the workers, but my functions I want to call are not inside my GetxController, but inside my class/widget with the build method.
Ok well you can do as @ertgrull suggested and put the standard listener syntax, or ever function in the constructor or build method. What function are you trying to call? What are you trying to do? Because unless you're actually displaying the index or need a rebuild of your bottom nav bar, you don't need that Obx there. You can still access the controller without it.
I have a bottom sheet inside my scaffold and whenever the index of my bottomnavigation changes, the bottomsheet should close or open (doing that with a controller). I think i need the Obx for showing the right color of my bottomNavigationItem, right? (see my inital example)
Ok, you could even put your showModalBottomSheet in the Getx class and pass in Get.context and it will work the same. And yeah sorry you're correct about the Obx needed for selected item color if you're gonna be in a stateless widget.
I pun an obx variable inside a function (inside a Getx controller) and the function became reactive without using workers - is it possible? feels like something happand behind the scenes...
0

if you want to use Obx you have to use Streams first create a Controller

in this example i want to just listen to changes of a number

class AutoLoginCheck extends GetxController {
   var num= "0".obs;

void checkNum() {
    if (num.value == "1") {
      num.value = "0";
    } else {
      num.value = "1";
    }
  }

we can use this function to call that number changing

onTap: () {
   num.checkLoginVal();
                            },

1 Comment

I updated my question with my controller. I do not want to call a function inside my controller, but to call a local function of my screen with the bottomnavigationbar. In my previous project I used ProviderListener from Riverpod for this. But this time I want to use GetX.
0

If you are using a GetxController, you can listen to the value in the controller anywhere by adding .obs to the value you would like to listen to.

After adding .obs in the controller the method to listen is the same as the accepted answer.

1 Comment

Bro, please elaborate your answer with some sample codes so that all people who read your answer fully understand what you mean. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.