flutterdartflutter-go-router

GoRouter triggering rebuild when navigating away


I've noticed that when I include final String _ = GoRouterState.of(context) in my Widget.build override, it will get notified of a change when navigating to a different page, forcing a unexpected rebuild.

For example, I have a Widget like:

class PageOne extends WatchingStatefulWidget {
  const PageOne({super.key});

  @override
  GameViewState createState() => PageOneState();
}

class PageOneState extends State<PageOne> {
  @override
  Widget build(BuildContext context) {
    print("building page one");
    final _ = GoRouterState.of(context);
  }
}

This widget represents a destination on the router. When navigated to I grab the extras from the GoRouterState.of(context).extras`.

This behaves as expected.

However, if I then call goNamed("somewhere-else"), I see that the MyWidgetState.build is re-triggered as the router context (I presume) is dirty.

You'll see the output like:

[GoRouter] going to /page-one
flutter: building page one
[GoRouter] going to /page-two
flutter: building page one
flutter: building page two

Is there a way to prevent page one being called when navigating away, as it produces an inconsistent state in my application code. Where the model believes the user has transitioned, but page one is called again and will try to be built on an inconsistent state.


Solution

  • This was triaged as a bug, for anyone who sees the same issue: https://github.com/flutter/flutter/issues/170255