1
 bool isChecked = false;

  void isValidate() {
    if (_formKey.currentState != null) {
      if (_formKey.currentState!.validate()) {
        print("is valid");
        Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(
              builder: (context) => AnaSayfaView(),
            ),
            (route) => false);
      } else {
        print("is not valid");
      }
    }
  }

I have this code. The error message says:

A page-based route cannot be completed using imperative api, provide a new list without the corresponding Page to Navigator.pages instead.'package:flutter/src/widgets/navigator.dart':navigator.dart:1Failed assertion: line 3040 pos 7: '!pageBased || isWaitingForExitingDecision'

I have tried to navigate another page and not turning back to same page again.

2 Answers2

0

How the PushAndRemoveUntil function works is that it keeps calling the method until when the result gets a true result. Since you are always sending false as the result it will not work.

Try the following - it will remove remove all page up to the first one and then push that page.

  bool isChecked = false;
  void isValidate() {
    if (_formKey.currentState != null) {
      if (_formKey.currentState!.validate()) {
        print("is valid");
        Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(
              builder: (context) => AnaSayfaView(),
            ),
            (route) => route.isFirst);
      } else {
        print("is not valid");
      }
    }
  }
rmtmckenzie
  • 37,718
  • 9
  • 112
  • 99
Mohammad_Asef
  • 292
  • 2
  • 17
  • 1
    your answers might be technically correct, but you need to expand them into something that answers what the broader community might be asking. I realize that being english as a second language makes things more difficult, but unfortunately there is a threshold you need to pass to make your answers useful, even if they are correct. Starting a sentence with 'bro' is not something that should be done in stackoverflow. I'm not trying to be an asshole, just to help you - any effort to answer questions is greatly appreciated, so thank you! – rmtmckenzie Jun 14 '23 at 07:37
  • I tried this but still the same result – Ömer Önder Jun 14 '23 at 14:00
0

Try this

 WidgetsBinding.instance!.addPostFrameCallback((_) {
    Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(
              builder: (context) => AnaSayfaView(),
            ),
            (route) => false);
    });
YsfKcr
  • 50
  • 1
  • 7