3

I'm using go_*router to navigate between screens unfortunately it causes every time rebuilds on the current page and the page I navigate to. On most pages I read documents from Firebase which causes unnecessary reads. As alternative I use the Navigator to achive navigating between pages without rebuilds. Is it possible to achive the same effect with go_*router?

Navigator.push(context,MaterialPageRoute(builder: (context) => SecondScreen(),); (rebuild on screen2)
context.go("/secondScreen") (rebuilds on screen1 and screen2)

Instead of go_router I would use Navigator.

Julius Guilt
  • 31
  • 1
  • 2

1 Answers1

0

I had the same problem I believe. When I use context.push() or context.pop() all the pages' states in the stack tree were running again which caused to lose states of pages that already exist. What I realized is the difference between pageBuilder & builder methods. In order to use pageBuilder method, you need to call MaterialPage<void> which will build the page each time.

Solution: Using builder attinstead pageBuilder

Instead of using pageBuilder;

GoRoute(
        path: '/',
        pageBuilder: (context, state) => MaterialPage<void>(
          key: ValueKey<String>(state.fullPath! +
              DateTime.now().millisecondsSinceEpoch.toString()),
          child: HomePage(),
        ),
      ),

Use builder;

GoRoute(
        path: '/',
        builder: (context, state) {
          return HomePage();
        },
      ),