Search code examples
flutterflutter-getx

using TextEditingController observable text never change inside init onInit ever()?


I am using GetX. I need once the user type a letter I get this letter to associate it to an object. But the issues is when I use ever function inside onInit fuction inside the controller, there is no change happen. so the even function never implemented.

The Controller Class Code Is:

class RegistrationController extends GetxController {
  // Email
  late Rx<TextEditingController> emailEditionController;
  Rx<Email>? email;
  @override
  void onInit() {
    emailEditionController = TextEditingController().obs;
    ever(
      emailEditionController,
      (_) {
        print("\n checked \n");
        return email = Email(email: emailEditionController.value.text).obs;
      },
    );
    super.onInit();
  }
}

Solution

  • onInit fires when your controller gets initialize only for once that will not call again until you force it or reinitialize your controller. So put your functions outside of it and Try using code below :

    class RegistrationController extends GetxController {
      // Email
      Rx<TextEditingController> emailEditionController = TextEditingController().obs;
      Rx<Email>? email;
      @override
      void onInit() {
        super.onInit();
      }
    
      ever(String val) {
        print("\n checked $val\n");
        return email?.value = Email(email: val); OR Email(email: emailEditionController.value.text);
      }
    }
    

    On TextField Side

    TextFormField(
                controller: cont.emailEditionController.value,
                onChanged: (val){
                  cont.ever(val);
                },
              )