flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
165.61k stars 27.34k forks source link

[go_router] Using the context.push, the route does not jump #138127

Open lizhuoyuan opened 11 months ago

lizhuoyuan commented 11 months ago

Is there an existing issue for this?

Steps to reproduce

  1. Click "push login" in "A Screen" to jump to the login page
  2. Click "login"

Expected results

Jump to the homepage and call the afterLogin method

Actual results

nothing happened

Code sample

Code sample ```dart // Copyright 2013 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; final GlobalKey _rootNavigatorKey = GlobalKey(debugLabel: 'root'); final GlobalKey _shellNavigatorKey = GlobalKey(debugLabel: 'shell'); void main() { runApp(ShellRouteExampleApp()); } class ShellRouteExampleApp extends StatelessWidget { ShellRouteExampleApp({super.key}); final GoRouter _router = GoRouter( navigatorKey: _rootNavigatorKey, initialLocation: '/a', debugLogDiagnostics: true, routes: [ StatefulShellRoute.indexedStack( builder: (context, state, navigationShell) { return ScaffoldWithNavBar( child: navigationShell, ); }, branches: [ StatefulShellBranch(routes: [ GoRoute( path: '/a', name: 'a', builder: (BuildContext context, GoRouterState state) { return ScreenA( login: bool.parse( state.uri.queryParameters['login'] ?? 'false'), ); }, ), ]), StatefulShellBranch(routes: [ GoRoute( path: '/b', builder: (BuildContext context, GoRouterState state) { return const ScreenB(); }, ), ]), ]), GoRoute( path: '/login', name: 'login', parentNavigatorKey: _rootNavigatorKey, builder: (BuildContext context, GoRouterState state) { return const LoginScreen(); }, ), ], ); @override Widget build(BuildContext context) { return MaterialApp.router( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), routerConfig: _router, ); } } class ScaffoldWithNavBar extends StatelessWidget { const ScaffoldWithNavBar({ required this.child, super.key, }); /// The widget to display in the body of the Scaffold. /// In this sample, it is a Navigator. final Widget child; @override Widget build(BuildContext context) { return Scaffold( body: child, bottomNavigationBar: BottomNavigationBar( items: const [ BottomNavigationBarItem( icon: Icon(Icons.home), label: 'A Screen', ), BottomNavigationBarItem( icon: Icon(Icons.business), label: 'B Screen', ), ], currentIndex: _calculateSelectedIndex(context), onTap: (int idx) => _onItemTapped(idx, context), ), ); } static int _calculateSelectedIndex(BuildContext context) { final String location = GoRouterState.of(context).uri.toString(); if (location.startsWith('/a')) { return 0; } if (location.startsWith('/b')) { return 1; } if (location.startsWith('/c')) { return 2; } return 0; } void _onItemTapped(int index, BuildContext context) { switch (index) { case 0: GoRouter.of(context).go('/a'); break; case 1: GoRouter.of(context).go('/b'); break; case 2: GoRouter.of(context).go('/c'); break; } } } class ScreenA extends StatefulWidget { final bool login; const ScreenA({super.key, required this.login}); @override State createState() => _ScreenAState(); } class _ScreenAState extends State { @override void initState() { super.initState(); afterLogin(); } void afterLogin() { print('login success :${widget.login}'); //todo ... setState((){}); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ const Text('Screen A'), TextButton( onPressed: () { context.push('/login'); }, child: const Text('push login'), ), ], ), ), ); } } /// The second screen in the bottom navigation bar. class ScreenB extends StatelessWidget { /// Constructs a [ScreenB] widget. const ScreenB({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ const Text('Screen B'), TextButton( onPressed: () { GoRouter.of(context).go('/b/details'); }, child: const Text('View B details'), ), ], ), ), ); } } /// The third screen in the bottom navigation bar. class LoginScreen extends StatelessWidget { /// Constructs a [LoginScreen] widget. const LoginScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ const Text('login'), TextButton( onPressed: () { GoRouter.of(context) .pushNamed('a', queryParameters: {'login': 'true'}); }, child: const Text('login'), ), ], ), ), ); } } ```

Screenshots or Video

Screenshots / Video demonstration https://github.com/flutter/flutter/assets/15923901/4b9cdebb-829f-43e6-acc8-89d792648de6

Logs

Logs ```console Performing hot restart... Syncing files to device Pixel 7 Pro... Restarted application in 1,094ms. [GoRouter] Full paths for routes: => /a => /b => /login known full paths for route names: a => /a login => /login I/flutter (31485): login success :false [GoRouter] setting initial location /a [GoRouter] Using MaterialApp configuration [GoRouter] pushing /login [GoRouter] getting location for name: "a", queryParameters: {login: true} [GoRouter] pushing /a?login=true [GoRouter] getting location for name: "a", queryParameters: {login: true} [GoRouter] pushing /a?login=true [GoRouter] getting location for name: "a", queryParameters: {login: true} [GoRouter] pushing /a?login=true [GoRouter] getting location for name: "a", queryParameters: {login: true} [GoRouter] pushing /a?login=true [GoRouter] getting location for name: "a", queryParameters: {login: true} [GoRouter] pushing /a?login=true [GoRouter] getting location for name: "a", queryParameters: {login: true} [GoRouter] pushing /a?login=true ```

Flutter Doctor output

Doctor output ```console flutter doctor -v [✓] Flutter (Channel stable, 3.13.9, on macOS 14.0 23A344 darwin-x64, locale zh-Hans-CN) • Flutter version 3.13.9 on channel stable at /Users/lizhuoyuan/fvm/versions/stable • Upstream repository https://github.com/flutter/flutter.git • Framework revision d211f42860 (2 weeks ago), 2023-10-25 13:42:25 -0700 • Engine revision 0545f8705d • Dart version 3.1.5 • DevTools version 2.25.0 • Pub download mirror https://pub.flutter-io.cn • Flutter download mirror https://storage.flutter-io.cn [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/lizhuoyuan/Library/Android/sdk • Platform android-33, build-tools 30.0.3 • ANDROID_HOME = /Users/lizhuoyuan/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.0.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15A507 • CocoaPods version 1.12.1 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2022.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • android-studio-dir = /Applications/Android Studio.app • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231) [✓] VS Code (version 1.83.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.74.0 [✓] Connected device (2 available) • Pixel 7 Pro (mobile) • 2A111FDH3006NG • android-arm64 • Android 14 (API 34) • Chrome (web) • chrome • web-javascript • Google Chrome 119.0.6045.105 [✓] Network resources • All expected network resources are available. • No issues found! ```
lizhuoyuan commented 11 months ago

If I use context.go, I can jump to the home page, but in this case I don't know how to call the afterlogin method.

dam-ease commented 11 months ago

Hi @lizhuoyuan. Thanks for filing this. This seems to be an issue with implementation and can't seem to point out the exact issue. Can you check out the sample in https://github.com/flutter/flutter/issues/122038#issue-1612087725 to see if it helps provide more insight?

dam-ease commented 11 months ago

Hi @lizhuoyuan. Thanks for filing this. This seems to be an issue with implementation and can't seem to point out the exact issue. Can you check out the sample in https://github.com/flutter/flutter/issues/122038#issue-1612087725 to see if it helps provide more insight?

lizhuoyuan commented 11 months ago

Hi @lizhuoyuan. Thanks for filing this. This seems to be an issue with implementation and can't seem to point out the exact issue. Can you check out the sample in #122038 (comment) to see if it helps provide more insight?

Thanks, I checked that question, but the situation is different from mine. My request is to jump to the ShellRoute page and call the page's method to process the passed parameters, but go will not go through the life cycle, except build, but there is no such thing in the build. It is suitable for calling methods because it may lead to repeated builds and push cannot switch routes.

dam-ease commented 11 months ago

Thanks for your response. I can reproduce this on the latest master channel as well.

flutter doctor -v

``` [!] Flutter (Channel master, 3.17.0-5.0.pre.64, on macOS 14.0 23A344 darwin-arm64, locale en-NG) • Flutter version 3.17.0-5.0.pre.64 on channel master at /Users/damilolaalimi/fvm/versions/master ! Warning: `dart` on your path resolves to /opt/homebrew/Cellar/dart/3.1.5/libexec/bin/dart, which is not inside your current Flutter SDK checkout at /Users/damilolaalimi/fvm/versions/master. Consider adding /Users/damilolaalimi/fvm/versions/master/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 2e088178e8 (7 hours ago), 2023-11-12 18:45:26 -0500 • Engine revision a18ee3c7f5 • Dart version 3.3.0 (build 3.3.0-120.0.dev) • DevTools version 2.30.0-dev.1 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/damilolaalimi/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • ANDROID_HOME = /Users/damilolaalimi/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.0.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15A507 • CocoaPods version 1.12.1 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2022.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694) [!] Android Studio (version unknown) • Android Studio at /Users/damilolaalimi/Downloads/Android Studio Preview.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart ✗ Unable to determine Android Studio version. • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) [✓] VS Code (version 1.84.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.50.0 [✓] VS Code (version 1.83.1) • VS Code at /Users/damilolaalimi/Downloads/Visual Studio Code.app/Contents • Flutter extension version 3.50.0 [✓] Connected device (4 available) • Damilola’s iPhone (mobile) • 00008110-001964480AE1801E • ios • iOS 17.0.2 21A351 • iPhone 14 Pro (mobile) • 6548B0D1-7674-4306-9852-198C465F1104 • ios • com.apple.CoreSimulator.SimRuntime.iOS-16-4 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 14.0 23A344 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 119.0.6045.123 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 2 categories. ```

chunhtai commented 11 months ago

may have the same root cause https://github.com/flutter/flutter/issues/111842

flutter-triage-bot[bot] commented 7 months ago

This issue is marked P1 but has had no recent status updates.

The P1 label indicates high-priority issues that are at the top of the work list. This is the highest priority level a bug can have if it isn't affecting a top-tier customer or breaking the build. Bugs marked P1 are generally actively being worked on unless the assignee is dealing with a P0 bug (or another P1 bug). Issues at this level should be resolved in a matter of months and should have monthly updates on GitHub.

Please consider where this bug really falls in our current priorities, and label it or assign it accordingly. This allows people to have a clearer picture of what work is actually planned. Thanks!

flutter-triage-bot[bot] commented 1 month ago

This issue is assigned to @chunhtai but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks!