0

When navigating from one page to another using GetX Flutter, the arguments passed from the first page are not received on the second page.

First Controller:

class LoginController extends GetxController {
  var _loading = false.obs;

  get loading => _loading.value;

  set loading(value) {
    _loading.value = value;
  }

  var _loginResponse = LoginResponse().obs;

  late TextEditingController emailController, passwordController;

  @override
  void onInit() {
    super.onInit();
    emailController = TextEditingController();
    passwordController = TextEditingController();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
    emailController.dispose();
    passwordController.dispose();
  }

  Future<void> loginCall(
      {required String email, required String password}) async {

 
    if (!GetUtils.isEmail(email)) {
      Get.snackbar('Invalid email', 'Please enter valid email',
          snackPosition: SnackPosition.BOTTOM);
      return;
    }

    if (password.length < 8) {
      Get.snackbar('Invalid password', 'Minimum 8 chars',
          snackPosition: SnackPosition.BOTTOM);
      return;
    }

    _loading.value = true;

    var result =
        await NetworkRequest.loginCall(email: email, password: password);

    result != null
        ? _loginResponse.value = result
        : Get.snackbar(
            'Something went wrong', 'Hold back and try again after sometime.',
            snackPosition: SnackPosition.BOTTOM);

    if(_loginResponse.value.responseCode == 1) {

      **Get.offAll(GoogleAuthenticationScreen(), arguments: _loginResponse.value.responseData!.session.toString());**
    }

    _loading.value = false;

  }
}

Second Controller:

class GoogleAuthenticationController extends GetxController {
  var _loading = false.obs;
  late TextEditingController pinController;
  var _authResponse = GoogleTwoFactorResponse().obs;

  @override
  void onInit() {
    super.onInit();
    pinController = TextEditingController();
    var session = Get.arguments.toString();
    twoFactorCall(token: session);
  }

  @override
  void onClose() {
    super.onClose();
    pinController.dispose();
  }

  Future<void> twoFactorCall({required String token}) async {
    _loading.value = true;

    var result = await NetworkRequest.googleAuthentication(token: token);

    result != null
        ? _authResponse.value = result
        : Get.snackbar(
            'Something went wrong', 'Hold back and try again after sometime.',
            snackPosition: SnackPosition.BOTTOM);

    Get.snackbar(result!.responseMessage!, "",
        snackPosition: SnackPosition.BOTTOM);

    _loading.value = false;
  }
}

Getx Version get: ^4.3.8

Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 2.2.2, on macOS 11.4 20F71 darwin-x64, locale en-IN) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) [✓] Xcode - develop for iOS and macOS [✓] Chrome - develop for the web [✓] Android Studio (version 4.1) [✓] Connected device (2 available)

• No issues found!

Kanwarpreet Singh
  • 2,037
  • 1
  • 13
  • 28

2 Answers2

0

you can try

 final session = Get.arguments;

outside of onInIt method. hope it will work

Alwayss Bijoy
  • 778
  • 9
  • 15
-1

Yes, I have an answer. I have used pass arguments from one class to another, show code below.

Here LoginController use for LoginScreen class

class LoginController extends GetxController
{
  passArguments()
  {
    Get.offAll(GoogleAuthenticationScreen(),arguments: "passData1");
    //For pass multiple arguments
    Get.offAll(GoogleAuthenticationScreen(),arguments:["passData1","passData2","passData3"]);
  }
}

Here your argument pass in GoogleAuthenticationScreen. And now you have to pass the argument from GoogleAuthenticationScreen to GoogleAuthenticationController.

class GoogleAuthenticationScreen extends StatelessWidget 
{
  GoogleAuthenticationController googleAuthenticationController = Get.put(GoogleAuthenticationController());

  @override
  Widget build(BuildContext context) {
    if(Get.arguments != null){
       //for one value
      googleAuthenticationController.arguments1.value = Get.arguments;
      //for multiple value
      googleAuthenticationController.arguments1.value = Get.arguments[0...1];
    }
    return Container();
  }
}

class GoogleAuthenticationController extends GetxController
{
  //here my value as a string data type
  var arguments1 = "".obs;
}

Try this way.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31