flutter [go_router] 在导航时使用push调用了web重定向,但未执行,

0kjbasz6  于 10个月前  发布在  Flutter
关注(0)|答案(2)|浏览(163)

重现步骤

  1. 运行 dart run build_runner build --delete-conflicting-outputs 以生成路由。
  2. 转到身份验证屏幕。
  3. LoginButton
  4. 按浏览器的后退箭头。
  5. 按浏览器的前进箭头。

预期结果

重定向到 Home 屏幕。

实际结果

重定向被调用,但没有发生重定向。
当使用推送导航到登录屏幕并使用浏览器的导航箭头向前移动时,重定向被调用但不执行。然而,如果我使用按钮访问登录屏幕,重定向按预期工作。
如您所见,重定向被调用,但没有发生重定向:

代码示例

代码示例

  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:go_router/go_router.dart';
  4. import 'package:provider/provider.dart';
  5. part 'main.g.dart';
  6. class LoginInfo extends ChangeNotifier {
  7. String _userName = '';
  8. String get userName => _userName;
  9. bool get loggedIn => _userName.isNotEmpty;
  10. void login(String userName) {
  11. _userName = userName;
  12. notifyListeners();
  13. }
  14. void logout() {
  15. _userName = '';
  16. notifyListeners();
  17. }
  18. }
  19. void main() => runApp(App());
  20. final LoginInfo loginInfo = LoginInfo();
  21. class App extends StatelessWidget {
  22. App({super.key});
  23. static const String title = 'GoRouter Example: Named Routes';
  24. @override
  25. Widget build(BuildContext context) => ChangeNotifierProvider<LoginInfo>.value(
  26. value: loginInfo,
  27. child: MaterialApp.router(
  28. routerConfig: _router,
  29. title: title,
  30. debugShowCheckedModeBanner: false,
  31. ),
  32. );
  33. late final GoRouter _router = GoRouter(
  34. debugLogDiagnostics: true,
  35. routes: $appRoutes,
  36. // changes on the listenable will cause the router to refresh it's route
  37. refreshListenable: loginInfo,
  38. );
  39. }
  40. @TypedGoRoute<HomeRoute>(
  41. path: '/',
  42. routes: [
  43. TypedGoRoute<LoginRoute>(
  44. path: 'login',
  45. )
  46. ],
  47. )
  48. class HomeRoute extends GoRouteData {
  49. const HomeRoute();
  50. @override
  51. Widget build(BuildContext context, GoRouterState state) => Scaffold(
  52. appBar: AppBar(
  53. title: const Text('Home Screen'),
  54. automaticallyImplyLeading: false,
  55. centerTitle: true,
  56. ),
  57. body: Center(
  58. child: TextButton(
  59. onPressed: () => context.push(const LoginRoute().location), // using go works as except
  60. child: const Text('Go to Auth')),
  61. ),
  62. );
  63. }
  64. class LoginRoute extends GoRouteData {
  65. const LoginRoute();
  66. @override
  67. FutureOr<String?> redirect(BuildContext context, GoRouterState state) {
  68. if (loginInfo.loggedIn) {
  69. return const HomeRoute().location;
  70. } else {
  71. return null;
  72. }
  73. }
  74. @override
  75. Widget build(BuildContext context, GoRouterState state) => Scaffold(
  76. appBar: AppBar(
  77. automaticallyImplyLeading: false,
  78. title: const Text('Login Screen'),
  79. centerTitle: true,
  80. ),
  81. body: Center(
  82. child: ElevatedButton(
  83. onPressed: () => context.read<LoginInfo>().login('test-user'),
  84. child: const Text('Login'),
  85. ),
  86. ),
  87. );
  88. }

截图或视频

截图/视频演示

视频:

Screencast.from.28-05-24.10.19.37.webm

日志

日志

  1. [Paste your logs here]

Flutter Doctor输出

Doctor输出

  1. [✓] Flutter (Channel stable, 3.19.6, on Ubuntu 22.04.4 LTS 6.5.0-35-generic, locale en_US.UTF-8)
  2. Flutter version 3.19.6 on channel stable at /home/user/fvm/versions/3.19.6
  3. Upstream repository https://github.com/flutter/flutter.git
  4. Framework revision 54e66469a9 (6 weeks ago), 2024-04-17 13:08:03 -0700
  5. Engine revision c4cd48e186
  6. Dart version 3.3.4
  7. DevTools version 2.31.1
  8. [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
  9. Android SDK at /media/..../
  10. Platform android-34, build-tools 34.0.0
  11. Java binary at: /home/user/android-studio/jbr/bin/java
  12. Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
  13. All Android licenses accepted.
  14. [✓] Chrome - develop for the web
  15. Chrome at google-chrome
  16. [✗] Linux toolchain - develop for Linux desktop
  17. clang++ is required for Linux development.
  18. It is likely available from your distribution (e.g.: apt install clang), or can be downloaded from https://releases.llvm.org/
  19. CMake is required for Linux development.
  20. It is likely available from your distribution (e.g.: apt install cmake), or can be downloaded from https://cmake.org/download/
  21. ninja is required for Linux development.
  22. It is likely available from your distribution (e.g.: apt install ninja-build), or can be downloaded from https://github.com/ninja-build/ninja/releases
  23. pkg-config is required for Linux development.
  24. It is likely available from your distribution (e.g.: apt install pkg-config), or can be downloaded from
  25. https://www.freedesktop.org/wiki/Software/pkg-config/
  26. [✓] Android Studio (version 2023.3)
  27. Android Studio at /home/andry/android-studio/
  28. Flutter plugin version 79.0.2
  29. Dart plugin can be installed from:
  30. 🔨 https://plugins.jetbrains.com/plugin/6351-dart
  31. android-studio-dir = /home/user/android-studio/
  32. Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
  33. [!] Android Studio (version unknown)
  34. Android Studio at /home/andry/android-studio
  35. Flutter plugin can be installed from:
  36. 🔨 https://plugins.jetbrains.com/plugin/9212-flutter
  37. Dart plugin can be installed from:
  38. 🔨 https://plugins.jetbrains.com/plugin/6351-dart
  39. Unable to determine Android Studio version.
  40. Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
  41. [✓] VS Code (version 1.89.1)
  42. VS Code at /usr/share/code
  43. Flutter extension version 3.88.0
  44. [✓] Connected device (2 available)
  45. Linux (desktop) linux linux-x64 Ubuntu 22.04.4 LTS 6.5.0-35-generic
  46. Chrome (web) chrome web-javascript Google Chrome 125.0.6422.112
  47. [✓] Network resources
  48. All expected network resources are available.
qq24tv8q

qq24tv8q1#

3.22.0上也存在同样的问题。

nvbavucw

nvbavucw2#

感谢报告。看到与报告中描述的相同行为。
稳定,运行 master flutter doctor -v

  1. [!] Flutter (Channel stable, 3.22.0, on macOS 12.2.1 21D62 darwin-x64, locale
  2. en-GB)
  3. Flutter version 3.22.0 on channel stable at
  4. /Users/dhs/documents/fluttersdk/flutter
  5. ! Warning: `flutter` on your path resolves to
  6. /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
  7. your current Flutter SDK checkout at
  8. /Users/dhs/documents/fluttersdk/flutter. Consider adding
  9. /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
  10. ! Warning: `dart` on your path resolves to
  11. /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
  12. current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
  13. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
  14. of your path.
  15. Upstream repository https://github.com/flutter/flutter.git
  16. Framework revision 5dcb86f68f (5 days ago), 2024-05-09 07:39:20 -0500
  17. Engine revision f6344b75dc
  18. Dart version 3.4.0
  19. DevTools version 2.34.3
  20. If those were intentional, you can disregard the above warnings; however
  21. it is recommended to use "git" directly to perform update checks and
  22. upgrades.
  23. [!] Xcode - develop for iOS and macOS (Xcode 12.3)
  24. Xcode at /Applications/Xcode.app/Contents/Developer
  25. ! Flutter recommends a minimum Xcode version of 13.
  26. Download the latest version or update via the Mac App Store.
  27. CocoaPods version 1.11.2
  28. [✓] Chrome - develop for the web
  29. Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
  30. [✓] VS Code (version 1.62.0)
  31. VS Code at /Applications/Visual Studio Code.app/Contents
  32. Flutter extension version 3.21.0
  33. [✓] Connected device (5 available)
  34. SM G975F (mobile) RZ8M802WY0X android-arm64 Android 11 (API 30)
  35. Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 •
  36. ios • iOS 14.4.1 18D61
  37. • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 •
  38. ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
  39. • macOS (desktop) • macos •
  40. darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64
  41. • Chrome (web) • chrome •
  42. web-javascript • Google Chrome 98.0.4758.80
  43. [✓] HTTP Host Availability
  44. • All required HTTP hosts are available
  45. ! Doctor found issues in 1 category.
  46. [!] Flutter (Channel master, 3.23.0-12.0.pre.7, on macOS 12.2.1 21D62
  47. darwin-x64, locale en-GB)
  48. • Flutter version 3.23.0-12.0.pre.7 on channel master at
  49. /Users/dhs/documents/fluttersdk/flutter
  50. ! Warning: `flutter` on your path resolves to
  51. /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
  52. your current Flutter SDK checkout at
  53. /Users/dhs/documents/fluttersdk/flutter. Consider adding
  54. /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
  55. ! Warning: `dart` on your path resolves to
  56. /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
  57. current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
  58. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
  59. of your path.
  60. • Upstream repository https://github.com/flutter/flutter.git
  61. • Framework revision 35ae519140 (2 hours ago), 2024-05-29 00:33:07 -0400
  62. • Engine revision b26e1b023c
  63. • Dart version 3.5.0 (build 3.5.0-191.0.dev)
  64. • DevTools version 2.36.0-dev.10
  65. • If those were intentional, you can disregard the above warnings; however
  66. it is recommended to use "git" directly to perform update checks and
  67. upgrades.
  68. [!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
  69. • Android SDK at /Users/dhs/Library/Android/sdk
  70. ✗ cmdline-tools component is missing
  71. Run `path/to/sdkmanager --install "cmdline-tools;latest"`
  72. See https://developer.android.com/studio/command-line for more details.
  73. ✗ Android license status unknown.
  74. Run `flutter doctor --android-licenses` to accept the SDK licenses.
  75. See https://flutter.dev/docs/get-started/install/macos#android-setup for
  76. more details.
  77. [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
  78. • Xcode at /Applications/Xcode.app/Contents/Developer
  79. • Build 13C100
  80. • CocoaPods version 1.11.2
  81. [✓] Chrome - develop for the web
  82. • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
  83. [✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2)
  84. • IntelliJ at /Applications/IntelliJ IDEA.app
  85. • Flutter plugin version 65.1.4
  86. • Dart plugin version 213.7228
  87. [✓] VS Code (version 1.62.0)
  88. • VS Code at /Applications/Visual Studio Code.app/Contents
  89. • Flutter extension version 3.29.0
  90. [✓] Connected device (3 available)
  91. • Darshan's iphone (mobile) 21150b119064aecc249dfcfe05e259197461ce23 ios
  92. iOS 15.3.1 19D52
  93. macOS (desktop) macos
  94. darwin-x64 macOS 12.2.1 21D62 darwin-x64
  95. Chrome (web) chrome
  96. web-javascript Google Chrome 109.0.5414.119
  97. [✓] Network resources
  98. All expected network resources are available.
  99. ! Doctor found issues in 1 category.
  100. [!] Xcode - develop for iOS and macOS (Xcode 12.3)
  101. Xcode at /Applications/Xcode.app/Contents/Developer
  102. ! Flutter recommends a minimum Xcode version of 13.
  103. Download the latest version or update via the Mac App Store.
  104. CocoaPods version 1.11.2
  105. [✓] Chrome - develop for the web
  106. Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
  107. [✓] VS Code (version 1.62.0)
  108. VS Code at /Applications/Visual Studio Code.app/Contents
  109. Flutter extension version 3.21.0
  110. [✓] Connected device (5 available)
  111. SM G975F (mobile) RZ8M802WY0X android-arm64 Android 11 (API 30)
  112. Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 •
  113. ios • iOS 14.4.1 18D61
  114. • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 •
  115. ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
  116. • macOS (desktop) • macos •
  117. darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64
  118. • Chrome (web) • chrome •
  119. web-javascript • Google Chrome 98.0.4758.80
  120. [✓] HTTP Host Availability
  121. • All required HTTP hosts are available
  122. ! Doctor found issues in 1 category.
展开查看全部

相关问题