android Flutter GetX:参数中传递的值在第二页上为null

jhkqcmku  于 2023-04-10  发布在  Android
关注(0)|答案(2)|浏览(149)

当使用GetX Flutter从一个页面导航到另一个页面时,从第一个页面传递的参数不会在第二个页面上接收。
第一控制器:

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;

  }
}

第二控制器:

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版本获取:^4.3.8

Doctor摘要(要查看所有详细信息,请运行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 web [✓] Android Studio(version 4.1)[✓]连接的设备(2可用)
·未发现问题!

f3temu5u

f3temu5u1#

你可以试试

final session = Get.arguments;

onInIt方法的外部。希望它能工作

unhi4e5o

unhi4e5o2#

是的,我有答案。我已经使用了从一个类到另一个类的传递参数,下面显示代码。
这里LoginController用于LoginScreen类

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

这里你的参数传入GoogleAuthenticationScreen。现在你必须将参数从GoogleAuthenticationScreen传递到GoogleAuthenticationController。

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

}

class GoogleAuthenticationController extends GetxController
{
   //here single value is obtained
   GoogleAuthenticationController({arguments1});
   //here my value as a string data type
   var arguments1 = "".obs;
   //here multiple values is obtained
   var arguments2 = [].obs;
}

这边请。

相关问题