0
votes

I don't use named routes in the app. Instead followed by the inspiration from FlutterBloc library I use route definitions like this.

class OrganizationPage extends StatelessWidget {
  static Route route() {
    return MaterialPageRoute<void>(builder: (_) => OrganizationPage());
  }

  // ....
}

And I navigate using...

Navigator.push(
  context,
  LoginPage.route(),
);

Assume from the point of loading the OrganizationPage I have navigated few times. I want a button press that will pop OrganizationPage route + every thing else on top of it and load a new page. How can this be done?

I thought of doing some thing like this...

Navigator.pushAndRemoveUntil(
  context,
  HomePage.route(),
  (Route<dynamic> route) {
    // print("Checking route: $route");
    // return route.builder.toString().contains("OrganizationPage")
    // `builder` is not available, even though it did in the debugger.
  },
);

Can what I want be done? If so how?

I assume if I use named routes it might be easier? Unfortunately I have written a lot of code and prefer to avoid a refactor. Also this way I can accept any args to a page easily.

Thanks in advance.

1

1 Answers

0
votes

Tried (Route<dynamic> route) => route.settings.name == "/OrganizationPage" ?

Update

Using the method highlighted in the question doesn't result a route name. But a route name can be provided explicitly.

  static Route route() {
    return MaterialPageRoute<void>(
      builder: (_) => OrganizationPage(),
      settings: RouteSettings(name: '/OrganizationPage'),
    );
  }

Now that the route has a name, we can use

Navigator.pushAndRemoveUntil(
  context,
  HomePage.route(),
  (Route<dynamic> route) => route.settings.name == OrganzationPage.route().settings.name;
);