Haaris Ahamed
Haaris Ahamed

Reputation: 367

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

Upvotes: 1

Views: 4920

Answers (2)

S. M. JAHANGIR
S. M. JAHANGIR

Reputation: 5060

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.

Upvotes: 3

Ravi Limbani
Ravi Limbani

Reputation: 1172

You can use below code for show snackbar in flutter

 Get.snackbar(
    "Hello",
    "Thank You!",
    snackPosition: SnackPosition.TOP,
    colorText: Colors.white,
    borderRadius: 10,
    backgroundColor:  AppColors.toastGreenColor ,
    icon: Image.asset(
      "assets/images/success_toast.png",
      height: 25,
      width: 25,
      color: Colors.white,
    ),
  );

Upvotes: 0

Related Questions