Search code examples
flutterflutter-getx

Flutter GetX setState


bool download=false; setState(() {download=true;});download==true?Text("new"):("old");

How to write this code by using get X without stateful widget.

Thank you;


Solution

  • first define a controller class

    class ControllerClass extends GetxController{
     RxBool download == false.obs;
    }
    

    in your stateless class you can state a set with two ways

    inject a controller first like this

     ControllerClass controller = Get.put(ControllerClass());
    

    First:

        Obx(()=>controller.download.value == true?Text("new"):("old"))
    

    in some function

    downloadCondition(){
       controller.download.value == // true or false;
    
    }
    

    Second:

        GetBuilder<ControllerClass>(builder: (controller){ return 
         controller.download.value == true? 
        Text("new"):("old"));}
    

    in some function

    downloadCondition(){
       controller.download.value == // true or false;
     controller.update();
    }