重现步骤
- 运行
dart run build_runner build --delete-conflicting-outputs
以生成路由。 - 转到身份验证屏幕。
- 按
LoginButton
。 - 按浏览器的后退箭头。
- 按浏览器的前进箭头。
预期结果
重定向到 Home
屏幕。
实际结果
重定向被调用,但没有发生重定向。
当使用推送导航到登录屏幕并使用浏览器的导航箭头向前移动时,重定向被调用但不执行。然而,如果我使用按钮访问登录屏幕,重定向按预期工作。
如您所见,重定向被调用,但没有发生重定向:
代码示例
代码示例
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
part 'main.g.dart';
class LoginInfo extends ChangeNotifier {
String _userName = '';
String get userName => _userName;
bool get loggedIn => _userName.isNotEmpty;
void login(String userName) {
_userName = userName;
notifyListeners();
}
void logout() {
_userName = '';
notifyListeners();
}
}
void main() => runApp(App());
final LoginInfo loginInfo = LoginInfo();
class App extends StatelessWidget {
App({super.key});
static const String title = 'GoRouter Example: Named Routes';
@override
Widget build(BuildContext context) => ChangeNotifierProvider<LoginInfo>.value(
value: loginInfo,
child: MaterialApp.router(
routerConfig: _router,
title: title,
debugShowCheckedModeBanner: false,
),
);
late final GoRouter _router = GoRouter(
debugLogDiagnostics: true,
routes: $appRoutes,
// changes on the listenable will cause the router to refresh it's route
refreshListenable: loginInfo,
);
}
@TypedGoRoute<HomeRoute>(
path: '/',
routes: [
TypedGoRoute<LoginRoute>(
path: 'login',
)
],
)
class HomeRoute extends GoRouteData {
const HomeRoute();
@override
Widget build(BuildContext context, GoRouterState state) => Scaffold(
appBar: AppBar(
title: const Text('Home Screen'),
automaticallyImplyLeading: false,
centerTitle: true,
),
body: Center(
child: TextButton(
onPressed: () => context.push(const LoginRoute().location), // using go works as except
child: const Text('Go to Auth')),
),
);
}
class LoginRoute extends GoRouteData {
const LoginRoute();
@override
FutureOr<String?> redirect(BuildContext context, GoRouterState state) {
if (loginInfo.loggedIn) {
return const HomeRoute().location;
} else {
return null;
}
}
@override
Widget build(BuildContext context, GoRouterState state) => Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: const Text('Login Screen'),
centerTitle: true,
),
body: Center(
child: ElevatedButton(
onPressed: () => context.read<LoginInfo>().login('test-user'),
child: const Text('Login'),
),
),
);
}
截图或视频
截图/视频演示
视频:
Screencast.from.28-05-24.10.19.37.webm
日志
日志
[Paste your logs here]
Flutter Doctor输出
Doctor输出
[✓] Flutter (Channel stable, 3.19.6, on Ubuntu 22.04.4 LTS 6.5.0-35-generic, locale en_US.UTF-8)
• Flutter version 3.19.6 on channel stable at /home/user/fvm/versions/3.19.6
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 54e66469a9 (6 weeks ago), 2024-04-17 13:08:03 -0700
• Engine revision c4cd48e186
• Dart version 3.3.4
• DevTools version 2.31.1
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at /media/..../
• Platform android-34, build-tools 34.0.0
• Java binary at: /home/user/android-studio/jbr/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
• All Android licenses accepted.
[✓] Chrome - develop for the web
• Chrome at google-chrome
[✗] Linux toolchain - develop for Linux desktop
✗ clang++ is required for Linux development.
It is likely available from your distribution (e.g.: apt install clang), or can be downloaded from https://releases.llvm.org/
✗ CMake is required for Linux development.
It is likely available from your distribution (e.g.: apt install cmake), or can be downloaded from https://cmake.org/download/
✗ ninja is required for Linux development.
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
✗ pkg-config is required for Linux development.
It is likely available from your distribution (e.g.: apt install pkg-config), or can be downloaded from
https://www.freedesktop.org/wiki/Software/pkg-config/
[✓] Android Studio (version 2023.3)
• Android Studio at /home/andry/android-studio/
• Flutter plugin version 79.0.2
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• android-studio-dir = /home/user/android-studio/
• Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
[!] Android Studio (version unknown)
• Android Studio at /home/andry/android-studio
• 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.10+0-17.0.10b1087.21-11572160)
[✓] VS Code (version 1.89.1)
• VS Code at /usr/share/code
• Flutter extension version 3.88.0
[✓] Connected device (2 available)
• Linux (desktop) • linux • linux-x64 • Ubuntu 22.04.4 LTS 6.5.0-35-generic
• Chrome (web) • chrome • web-javascript • Google Chrome 125.0.6422.112
[✓] Network resources
• All expected network resources are available.
2条答案
按热度按时间qq24tv8q1#
在
3.22.0
上也存在同样的问题。nvbavucw2#
感谢报告。看到与报告中描述的相同行为。
稳定,运行 master flutter doctor -v