I find an issue with my flutter
project with go_router
that the previous page is re-rendered when I navigate to another page with pushNamed
.
I have the following setup
HomePage: route path: `/`
-- ScreenWithUniqueKey: route path/name: `screen_with_unique_key`
-- ScreenWithoutUniqueKey: route path/name: `screen_without_unique_key`
-- -- DetailPage: route path/name: `detail_page`
I added a print statement in the build
function for each screen (not in HomePage) and here are the test results
- HomePage -> goNamed -> ScreenWithUniqueKey -> pushNamed -> DetailPage
build ScreenWithUniqueKey
build DetailPage
build ScreenWithUniqueKey
- HomePage -> goNamed -> ScreenWithUniqueKey -> pushReplacementNamed -> DetailPage
build ScreenWithoutUniqueKey
build DetailPage
- HomePage -> goNamed -> ScreenWithoutUniqueKey -> pushNamed -> DetailPage
build ScreenWithoutUniqueKey
build DetailPage
build ScreenWithoutUniqueKey
- HomePage -> goNamed -> ScreenWithoutUniqueKey -> pushReplacementNamed -> DetailPage
build ScreenWithoutUniqueKey
build DetailPage
Here is the example code and I run this code in https://dartpad.dev/
with Dart 3.4.1, Flutter 3.22.1 and go_router 14.1.4
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(const MyApp());
}
final GoRouter _router = GoRouter(
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (BuildContext context, GoRouterState state) {
return const HomeScreen();
},
routes: <RouteBase>[
GoRoute(
path: 'screen_with_unique_key',
name: 'screen_with_unique_key',
builder: (_1, _2) => ScreenWithUniqueKey(key: UniqueKey()),
),
GoRoute(
path: 'screen_without_unique_key',
name: 'screen_without_unique_key',
builder: (_1, _2) => ScreenWithoutUniqueKey(),
),
GoRoute(
path: 'detail_page',
name: 'detail_page',
builder: (_1, _2) => DetailPage(),
),
],
),
],
);
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
class HomeScreen extends StatelessWidget {
/// Constructs a [HomeScreen]
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home Screen')),
body: Center(
child: Column(
children: [
ElevatedButton(
onPressed: () => context.go('/screen_with_unique_key'),
child: const Text('Go to the Screen With Unique Key'),
),
ElevatedButton(
onPressed: () => context.go('/screen_without_unique_key'),
child: const Text('Go to the Screen Without Unique Key'),
),
],
),
),
);
}
}
class ScreenWithUniqueKey extends StatelessWidget {
const ScreenWithUniqueKey({super.key});
@override
Widget build(BuildContext context) {
print('build ScreenWithUniqueKey');
return Scaffold(
appBar: AppBar(title: const Text('Screen With Unique Key')),
body: Center(
child: Column(
children: [
ElevatedButton(
onPressed: () => context.pushNamed('detail_page'),
child: const Text('Push Named to Detail Page'),
),
ElevatedButton(
onPressed: () => context.pushReplacementNamed('detail_page'),
child: const Text('Push Reaplcement Named to Detail Page'),
),
],
),
),
);
}
}
class ScreenWithoutUniqueKey extends StatelessWidget {
const ScreenWithoutUniqueKey({super.key});
@override
Widget build(BuildContext context) {
print('build ScreenWithoutUniqueKey');
return Scaffold(
appBar: AppBar(title: const Text('Screen Without Unique Key')),
body: Center(
child: Column(
children: [
ElevatedButton(
onPressed: () => context.pushNamed('detail_page'),
child: const Text('Push Named to Detail Page'),
),
ElevatedButton(
onPressed: () => context.pushReplacementNamed('detail_page'),
child: const Text('Push Reaplcement Named to Detail Page'),
),
],
),
),
);
}
}
class DetailPage extends StatelessWidget {
const DetailPage({super.key});
@override
Widget build(BuildContext context) {
print('build DetailPage');
return Scaffold(
appBar: AppBar(title: const Text('Detail Page')),
body: Center(
child: ElevatedButton(
onPressed: () => context.pop(),
child: const Text('Go back'),
),
),
);
}
}