Skip to content

Rebuild triggered when navigating away. #170255

Open
@jamesstidard

Description

@jamesstidard

Steps to reproduce

I'm questioning the behaviour of the GoRouter, and that it triggers a rebuild on a view which you transition away from, after you've left. This has made some hard to hunt down state inconsistency issue in the project I'm currently working in.

I feel like the behaviour would be unintentional, so reporting it as a bug.

The issue is, if you reference final _ = GoRouterState.of(context); in you Widget.build, then you will be subscribed to changes from the router state (as is my understanding). This is good when you are wanting to rebuild a view based on maybe the extras changing.

However, when navigating to a entirely different page using context.go, the previous view still gets rebuilt.

Expected results

I would expect the view to have been destroyed, and the build to not have been called again with a stale state.

Actual results

The previous view is called again with the context of how it was initially instantiated (the path of itself, and the previous extras)

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

final _router = GoRouter(
  debugLogDiagnostics: true,
  routes: [
    GoRoute(
      path: '/',
      builder: (context, state) => MyHomePage(title: "page-one"),
    ),
    GoRoute(
      path: '/page-two',
      builder: (context, state) => MyHomePage(title: "page-two"),
    ),
  ],
);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: _router,
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    print("building ${widget.title}");
    final _ = GoRouterState.of(context);
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Text(widget.title)
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => context.go("/page-two"),
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

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

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.29.2, on macOS 15.5 24F74 darwin-arm64, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 16.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.3)
[✓] VS Code (version 1.100.3)
[✓] Connected device (5 available)
[✓] Network resources

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    found in release: 3.32Found to occur in 3.32found in release: 3.33Found to occur in 3.33frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.team-frameworkOwned by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions