Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[go_router] location does not update properly when .go() is called for more than two depth #117671

@serendipity1004

Description

@serendipity1004

Steps to Reproduce

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

void main() {
  runApp(
    RootScreen(),
  );
}

final router = GoRouter(
  initialLocation: '/',
  routes: [
    GoRoute(
      path: '/',
      builder: (_, __) => ScreenOne(),
      routes: [
        GoRoute(
          path: 'two',
          builder: (_, __) => ScreenTwo(),
          routes: [
            GoRoute(
              path: 'three',
              builder: (_, __) => ScreenThree(),
            ),
          ],
        ),
      ],
    ),
  ],
);

class RootScreen extends StatelessWidget {
  const RootScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: router,
    );
  }
}

class ScreenOne extends StatelessWidget {
  const ScreenOne({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final router = GoRouter.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text(router.location),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            context.go('/two');
          },
          child: Text('Go'),
        ),
      ),
    );
  }
}

class ScreenTwo extends StatelessWidget {
  const ScreenTwo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(router.location),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            context.go('/two/three');
          },
          child: Text('Go'),
        ),
      ),
    );
  }
}

class ScreenThree extends StatelessWidget {
  const ScreenThree({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(router.location),
      ),
      body: Center(
        child: Text('three'),
      ),
    );
  }
}

the above code has three nested routes

  • /
  • /two
  • /two/three

I would expect that if I pop in following order /two/three -> /two -> / GoRouter.of(context).location would also reflect the correct route location (/two/three -> /two -> /); however, the second pop() does not reflect correct location (/two/three -> /two/ -> /two/). The Appbar in the following video shows GoRouter.of(context).location value. The first pop() and second pop() results in same route location. If I just push one route and pop() the location gets correctly reset.

Simulator.Screen.Recording.-.iPhone.13.Pro.Max.-.2022-12-27.at.16.44.50.mp4

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: regressionIt was better in the past than it is nowfound in release: 3.3Found to occur in 3.3found in release: 3.7Found to occur in 3.7has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions