Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[go_router] 5.2.1 goRouterState.path & goRouterState.name giving null value in route builders if sub routes in ShellRoute and control not going on defined route builder #116516

Open
hpomgithub opened this issue Dec 5, 2022 · 15 comments
Labels
found in release: 3.3 Found to occur in 3.3 found in release: 3.7 Found to occur in 3.7 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: go_router The go_router package P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels. team-go_router Owned by Go Router team triaged-go_router Triaged by Go Router team

Comments

@hpomgithub
Copy link

hpomgithub commented Dec 5, 2022

final GlobalKey<NavigatorState> _rootNavigatorKey =
    GlobalKey<NavigatorState>(debugLabel: 'root');
final GlobalKey<NavigatorState> _shellNavigatorKey =
    GlobalKey<NavigatorState>(debugLabel: 'shell');

void main() {
  usePathUrlStrategy();

  runApp(ShellRouteExampleApp());
}

/// An example demonstrating how to use [ShellRoute]
class ShellRouteExampleApp extends StatelessWidget {
  /// Creates a [ShellRouteExampleApp]
  ShellRouteExampleApp({Key? key}) : super(key: key);

  final GoRouter _router = GoRouter(
    navigatorKey: _rootNavigatorKey,
    initialLocation: '/a',
    routes: <RouteBase>[
      GoRoute(
          path: '/new',
          builder: (a, b) => Text('new'),
          parentNavigatorKey: _rootNavigatorKey),

      /// Application shell
      ShellRoute(
        navigatorKey: _shellNavigatorKey,
        pageBuilder:
            (BuildContext context, GoRouterState goRouterState, Widget child) {
              print(
                  "State name in Builder of shell builder:-${goRouterState.name}");
              print(
                  "State location  in Builder of shell builder:-${goRouterState.location}");
              print(
                  "State  path in Builder of shell builder:-  ${goRouterState.path} ");
          return MaterialPage(
              child: ScaffoldWithNavBar(child: child), maintainState: true);
        },
        routes: <RouteBase>[
          /// The first screen to display in the bottom navigation bar.
          GoRoute(
            name: 'a',
            path: '/a',
            builder: (BuildContext, goRouterState) {
              print(
                  "State name in Builder of route:-${goRouterState.name}");
              print(
                  "State location  in Builder of route:-${goRouterState.location}");
              print(
                  "State  path in Builder of route:-  ${goRouterState.path}");
              return Text("a");
            })])
)
]
)
}

CONSOLE OUTPUT :-
State name in Builder of shell builder:-null
State location  in Builder of shell builder:-/a
State  path in Builder of shell builder:-   
SUMMARY :-
-> IN OUTPUT ONLY SHELL BUILDER GIVES THE OUTPUT.
-> ROUTE BUILDER DEFINED FOR PATH /a ID NOT GETTING CALLED NOR EXECUTED ON CALLING THAT PARTICULAR PATH.
-> ALSO STATE NAME AND PATH IS NULL AND BLANK SPACE RESPECTIVELY.
@danagbemava-nc danagbemava-nc changed the title [go_router] 5.2.1 goRouterState.path & goRouterState.name giving null value in route builders if sub routes in ShellRoute and control not going on defined route builder #116489 [go_router] 5.2.1 goRouterState.path & goRouterState.name giving null value in route builders if sub routes in ShellRoute and control not going on defined route builder Dec 5, 2022
@darshankawar darshankawar added the in triage Presently being triaged by the triage team label Dec 6, 2022
@darshankawar
Copy link
Member

@hpomgithub
Can you provide flutter doctor -v and the code sample in proper format so that we can directly copy paste and run to verify the behavior ?
Make sure the code sample is complete but minimal.

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Dec 6, 2022
@hpomgithub
Copy link
Author

@darshankawar
The code is same as that of example in shell route documentation. The only difference is that of print statements which I have added to print the values of state.name and path.

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Dec 6, 2022
@hpomgithub
Copy link
Author

Flutter (Channel stable, 3.3.9, on macOS 12.4 21F79 darwin-arm, locale en-IN)
• Flutter version 3.3.9 on channel stable at /Users/yashgogna/FlutterDev/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision b8f7f1f (2 weeks ago), 2022-11-23 06:43:51 +0900
• Engine revision 8f2221fbef
• Dart version 2.18.5
• DevTools version 2.15.0

@darshankawar
Copy link
Member

Thanks for the update.

stable, master flutter doctor -v
[✓] Flutter (Channel stable, 3.3.9, on macOS 12.2.1 21D62 darwin-x64, locale
    en-GB)
    • Flutter version 3.3.9 on channel stable at
      /Users/dhs/documents/fluttersdk/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b8f7f1f986 (12 days ago), 2022-11-23 06:43:51 +0900
    • Engine revision 8f2221fbef
    • Dart version 2.18.5
    • DevTools version 2.15.0

[!] Xcode - develop for iOS and macOS (Xcode 12.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    ! Flutter recommends a minimum Xcode version of 13.
      Download the latest version or update via the Mac App Store.
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] VS Code (version 1.62.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.21.0

[✓] Connected device (5 available)
    • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
    • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
      ios            • iOS 14.4.1 18D61
    • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
      ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
    • macOS (desktop)            • macos                                    •
      darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
    • Chrome (web)               • chrome                                   •
      web-javascript • Google Chrome 98.0.4758.80

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.

[!] Flutter (Channel master, 3.7.0-3.0.pre.57, on macOS 12.2.1 21D62 darwin-x64,
    locale en-GB)
    • Flutter version 3.7.0-3.0.pre.57 on channel master at
      /Users/dhs/documents/fluttersdk/flutter
    ! Warning: `flutter` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
      your current Flutter SDK checkout at
      /Users/dhs/documents/fluttersdk/flutter. Consider adding
      /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
    ! Warning: `dart` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
      current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
      Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
      of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ef6ead4401 (44 minutes ago), 2022-12-06 23:15:24 -0500
    • Engine revision 67254d6e4b
    • Dart version 2.19.0 (build 2.19.0-467.0.dev)
    • DevTools version 2.20.0
    • If those were intentional, you can disregard the above warnings; however
      it is recommended to use "git" directly to perform update checks and
      upgrades.


[!] Xcode - develop for iOS and macOS (Xcode 12.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    ! Flutter recommends a minimum Xcode version of 13.
      Download the latest version or update via the Mac App Store.
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] VS Code (version 1.62.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.21.0

[✓] Connected device (5 available)
    • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
    • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
      ios            • iOS 14.4.1 18D61
    • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
      ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
    • macOS (desktop)            • macos                                    •
      darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
    • Chrome (web)               • chrome                                   •
      web-javascript • Google Chrome 98.0.4758.80

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.



@darshankawar darshankawar added p: first party package flutter/packages repository. See also p: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on p: go_router The go_router package found in release: 3.3 Found to occur in 3.3 found in release: 3.7 Found to occur in 3.7 and removed in triage Presently being triaged by the triage team labels Dec 7, 2022
@chunhtai
Copy link
Contributor

chunhtai commented Dec 8, 2022

I don't think there is a reasonable value to put into name and path since the ShellRoute does not have a name and path. If the goal is to know which sub route has the match, we should add some new property in the GoRouterState to expose that.

@hpomgithub
Copy link
Author

Hello @chunhtai , This was working fine in the version 5.0.1. The point I would like to bring into you notice is that it is not responding when a particular sub-route(/a) defined in Shell Route is called. To confirm this we have logged the values. As here you can see that when we call '/a' path then that particular go route builder function (in sub route of shell route) must be called, but it is not getting called and to know what's fishy we have logged the state.name and state.path in shell route builder which is giving us null. So this means internally name and path are not getting initialised when that particular '/a' is called.

@chunhtai
Copy link
Contributor

chunhtai commented Dec 9, 2022

I did see the log gets printed out

State name in Builder of shell builder:-null
State location  in Builder of shell builder:-/a
State  path in Builder of shell builder:-   
State name in Builder of route:-a
State location  in Builder of route:-/a
State  path in Builder of route:-  /a

This is the code i was using

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

final GlobalKey<NavigatorState> _rootNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'root');
final GlobalKey<NavigatorState> _shellNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'shell');

void main() {
  // usePathUrlStrategy();

  runApp(ShellRouteExampleApp());
}

/// An example demonstrating how to use [ShellRoute]
class ShellRouteExampleApp extends StatelessWidget {
  /// Creates a [ShellRouteExampleApp]
  ShellRouteExampleApp({Key? key}) : super(key: key);

  final GoRouter _router = GoRouter(
      navigatorKey: _rootNavigatorKey,
      initialLocation: '/a',
      routes: <RouteBase>[
  GoRoute(
  path: '/new',
      builder: (a, b) => Text('new'),
      parentNavigatorKey: _rootNavigatorKey),

  /// Application shell
      ShellRoute(
  navigatorKey: _shellNavigatorKey,
  pageBuilder:
  (BuildContext context, GoRouterState goRouterState, Widget child) {
  print(
  "State name in Builder of shell builder:-${goRouterState.name}");
  print(
  "State location  in Builder of shell builder:-${goRouterState.location}");
  print(
  "State  path in Builder of shell builder:-  ${goRouterState.path} ");
  return MaterialPage(
  child: Scaffold(body: child), maintainState: true);
  },
  routes: <RouteBase>[
  /// The first screen to display in the bottom navigation bar.
  GoRoute(
  name: 'a',
  path: '/a',
  builder: (BuildContext, goRouterState) {
  print(
  "State name in Builder of route:-${goRouterState.name}");
  print(
  "State location  in Builder of route:-${goRouterState.location}");
  print(
  "State  path in Builder of route:-  ${goRouterState.path}");
  return Text("a");
  })])
  ]
  );

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

This was working fine in the version 5.0.1

What is working in 5.0.1? The name and path? or the logging in sub-route?

@chunhtai chunhtai added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Dec 9, 2022
@hpomgithub
Copy link
Author

Hi @chunhtai Everything was working in 5.0.1, but issue came in all versions of 5.1.10 - 5.2.1. But let me check it using the way you did.
Thanks

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Dec 9, 2022
@hpomgithub
Copy link
Author

Hi @chunhtai ,
One thing more I have found out that if in your code if you replace the child widget in shell pagebuilder with Text('abc') widget then your route builder function of /a path will not get called..
REPLACE THIS LINE:
return MaterialPage(
child: Scaffold(body: child), maintainState: true);
WITH
return MaterialPage(
child: Scaffold(body: Text('abc')), maintainState: true);
..............
The thing here I am trying to understand is that whether this is an issue or an unknown concept behind ShellRoute working.

@chunhtai
Copy link
Contributor

chunhtai commented Dec 9, 2022

yes it is expected. The child represent the subtree of the sub-route. if you throw it away, there is no reason to build it

@hpomgithub
Copy link
Author

Hi @chunhtai ,
But how it is unable to call that particular route builder function attached with that URL path. In my application I was using indexed stack to save the state but after 5.1.10 that workaround stopped working. So is there any better way to keep the states saved of child widgets while navigating using bottom navigation in shell route builder.

@chunhtai
Copy link
Contributor

chunhtai commented Dec 9, 2022

take a look at this flutter/packages#2650

@hpomgithub
Copy link
Author

Hi @chunhtai ,
Can you also provide me the doc to properly implement stateful shell route and also is it safe for production app?

@hpomgithub
Copy link
Author

@chunhtai
From where to download the StatefulNestedDart as these dependency errors are shown.
The function 'StatefulShellBranch' isn't defined.
The function 'StatefulShellBranch' isn't defined....etc

@stuartmorgan stuartmorgan added the P2 Important issues not at the top of the work list label Dec 13, 2022
@ahmednfwela
Copy link

ahmednfwela commented Feb 15, 2023

adding another bug to this issue:
GoRouterState.of(context).name is always null no matter where it's used in the application
cc: @chunhtai

A workaround (currently produces invalid_use_of_visible_for_testing_member):

final route = GoRouter.of(context).routerDelegate.matches.last.route;
final routeName = route is GoRoute ? route.name : null;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
found in release: 3.3 Found to occur in 3.3 found in release: 3.7 Found to occur in 3.7 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: go_router The go_router package P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels. team-go_router Owned by Go Router team triaged-go_router Triaged by Go Router team
Projects
No open projects
Status: No status
Development

No branches or pull requests

6 participants