flutter go_router: re-render previous page when navigate with pushNamed

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'),
        ),
      ),
    );
  }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật