Search code examples
flutterflutter-getx

Show Snackbar Reactively Using GetX in Flutter


This is the scenario

  1. Making an API call
  2. Response is empty
  3. Need to show 'No results found' msg to the user through Snackbar from GetX controller
  4. But I don't want to pass the instance of the view class to the controller to show the Snackbar.
  5. Is there any Getx widget that listens to the msg.obs value in the controller and executes Get.Snackbar() code in the view

Solution

  • From my knowledge, I think unlike BLoC there's nothing like BlocListener or BlocConsumer in GetX. But you can use RxWorker to achieve this like:

    ever(someObservable, (){
    
        doSomething(); // show dialog, snackbar, navigate to other pages
    
     }
    

    Just remember you need to put this before the return of your build method.