'package:flutter/src/widgets/will_pop_scope.dart': Failed assertion: line 61 pos 12: '_route == ModalRoute.of(context)': is not true

Viewed 182

Got an error when getting back to previous screen.. Also i am getting

Duplicate GlobalKey detected in widget tree.

Screen A -> Screen B ->Screen c works fine

But when getting back from Screen C ->Screen A i face such issue

I have initialized global key as:

GlobalKey loginformKey = new GlobalKey(debugLabel: '_loginformKey');

Also tried making final but didn't work.I am using Getx for state management.Here is my Controller for Login.

class LoginController extends BaseController {
  final LoginRespostory repository;
  final LoginInterface loginInterface;
  GlobalKey<FormState> loginformKey;

  LoginController({@required this.repository, this.loginInterface})
      : assert(repository != null);
  TextEditingController emailController,
      passwordcontroller,
      phonenumberController;

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    emailController = TextEditingController();
    passwordcontroller = TextEditingController();
    phonenumberController = TextEditingController();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    loginformKey = null;
    emailController.dispose();
    passwordcontroller.dispose();
    phonenumberController.dispose();
  }

  @override
  void onConnected() {
    // TODO: implement onConnected
    super.onConnected();
  }

  @override
  void onDisconnect() {
    // TODO: implement onDisconnect
    super.onDisconnect();
  }

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    loginformKey = new GlobalKey<FormState>(debugLabel: '_loginformKey');
  }

  validateAndProceed() {
    if (formKey.currentState.validate()) {
      userlogin();
    } else {
      Utils.showErrorSnackBar(title: "Success", message: "validation error");
     }
  }

  Future<void> userlogin() async {
    Utils.showloading();
    return await loginInterface
        .getlogin(emailController.text, passwordcontroller.text)
        .then((value) => onSuccess(value))
        .catchError((error) => onError(error));
  }

  onSuccess(SocialLoginResponse loginresponse) {
    if (loginresponse.ok) {
      AppPrefernces.putString(
          AppPrefernces.LOGINRESPONSE, loginresponse.toString());
      AppPrefernces.putString(AppPrefernces.TOKEN, loginresponse.accessToken);

      if (!loginresponse.user.phoneVerified) {
      } else {
        Utils.dismissloading();
        Get.toNamed(Routes.DASHBOARD);
      }
    } else {
      Utils.dismissloading();
      Utils.showErrorSnackBar(
          title: loginresponse.ok.toString(), message: loginresponse.message);
    }
  }

  onError(error) {
    Utils.dismissloading();
    Utils.showErrorSnackBar(title: AppString.ERROR, message: AppString.ERROR);
  }

  onDetailSuccess(SocialLoginResponse response) {
    if (response.ok) {
      Utils.showSuccessSnackBar(
          title: response.ok.toString(), message: AppString.SUCCESS);
      AppPrefernces.putString(AppPrefernces.LOGINRESPONSE, response.toString());
      AppPrefernces.putString(AppPrefernces.TOKEN, response.accessToken);

      emailController.clear();
      passwordcontroller.clear();
      Future.delayed(
          Duration(seconds: 1), () => Get.offNamed(Routes.DASHBOARD));
    } else {
      Utils.showErrorSnackBar(
          title: response.ok.toString(), message: response.accessToken);
    }
  }
}

}
1 Answers

I also faced this issue and managed to solve this by moving the formKey from the Controller to the Widget itself. Also I converted the Widget from Stateless to Stateful.

It works now.

Hope this works for you too.

Related