Search code examples
flutterflutter-getx

Empty TextEditingController value after Get.offAllNamed with fenix: true


Problem: I get empty data from TextEditingController after returning to page with Get.offAllNamed and using fenix: true for binding

Login page has two TextFormFields tied with TextEditingControllers in the LoginLogic controller.

login_page.dart

/////
                     TextFormField(
                        controller: controller.usernameValueController,
                        validator: notEmptyValidator,
                        decoration: const InputDecoration(
                          border: OutlineInputBorder(),
                          labelText: 'Login',
                        ),
                      )
/////

login_logic.dart

class LoginLogic extends GetxController {
  var usernameValueController = TextEditingController();
  var passwordValueController = TextEditingController();
/////
  void login() async {

    var loginRequest = LoginRequest(usernameValueController.text, passwordValueController.text)
//retrieve and save auth token,  
/////
}

login_binding.dart

class LoginBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut<LoginLogic>(
          () => LoginLogic(), fenix: true
    );
  }
}

In case of auth token expiration user is transferred back to login with Get.offAllNamed method to prevent backstack navigation. But after login controller recreation TextEditingController doesn't receive any value from textfield.


Solution

  • The controller will be deleted if the screen that called the contoller disposed if you want permanent controller change lazyput to Get.put in the binding