我正在嘗試使用 GoRoute、Stream 和 Bloc。所以,我正在實施身份驗證流程。因此,如果用戶未登錄,他們將無法訪問任何頁面。并且,`Stream 允許不斷地收聽 AuthStatus (Bloc State)。但是,所以我不確定,為什么但我收到了這個錯誤
Exception: 'package:go_router/src/go_router_delegate.dart':
Failed assertion: line 299 pos 13: '!redirects.contains(redir)':
redirect loop detected:
我試圖創建一個虛擬應用程式來了解到底出了什么問題。但是,我無法弄清楚是什么原因。就在下面我附上了代碼,它只有大約 200 行,任何人都可以復制和粘貼它,然后運行命令flutter run
,螢屏上會出現一個應用程式錯誤。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
enum Pages {
home,
profile,
settings,
}
class FooStream {
final StreamController<Pages> _controller;
FooStream() : _controller = StreamController.broadcast();
Stream<Pages> get stream {
final listOfPages = [Pages.home, Pages.profile, Pages.settings];
// every 3 seconds loop over enum and emit a new value to the stream
Timer.periodic(const Duration(seconds: 3), (timer) {
final index = timer.tick ~/ 3;
if (index >= listOfPages.length) {
timer.cancel();
} else {
_controller.add(listOfPages[index]);
}
});
return _controller.stream;
}
void update(Pages page) {
_controller.add(page);
}
void close() {
_controller.close();
}
void dispose() {
_controller.close();
}
}
class FooNotifier extends ChangeNotifier {
final ValueNotifier<Pages> _page = ValueNotifier(Pages.home);
ValueNotifier<Pages> get page => _page;
// listen to the stream and whenever it emits a new value, update the page
final FooStream stream;
FooNotifier(this.stream) {
stream.stream.listen((Pages page) {
_page.value = page;
notifyListeners();
});
}
void close() {
notifyListeners();
}
@override
void dispose() {
notifyListeners();
super.dispose();
}
}
// my home page
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text(
"Home",
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
)),
);
}
}
// profile page
class ProfilePage extends StatelessWidget {
const ProfilePage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text(
"Profile",
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
)),
);
}
}
// setting page
class SettingPage extends StatelessWidget {
const SettingPage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text(
"Setting",
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
)),
);
}
}
class FooRoute {
final FooNotifier notifier;
FooRoute(this.notifier);
GoRouter routeTo() {
return GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const MyHomePage(),
),
GoRoute(
path: '/profile',
builder: (context, state) => const ProfilePage(),
),
GoRoute(
path: '/setting',
builder: (context, state) => const SettingPage(),
),
],
refreshListenable: notifier,
redirect: safePage,
debugLogDiagnostics: true,
);
}
// on page change, return the new page
String? safePage(GoRouterState state) {
final newPage = notifier.page.value;
// if the new page is the same as the current page, do nothing
if (newPage == Pages.home) {
return '/';
}
if (newPage == Pages.profile) {
return '/profile';
}
if (newPage == Pages.settings) {
return '/settings';
}
return null;
}
}
class FooApp extends StatelessWidget {
FooApp({
Key? key,
}) : super(key: key);
final _router = FooRoute(FooNotifier(FooStream())).routeTo();
// base the
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<Pages>(
valueListenable: FooNotifier(FooStream()).page,
builder: (context, value, child) {
return MaterialApp.router(
routerDelegate: _router.routerDelegate,
routeInformationParser: _router.routeInformationParser,
);
},
);
}
}
void main(List<String> args) {
runApp(FooApp());
}
我還附上了我在終端中收到的訊息。一旦上面的代碼運行。
Launching lib\main.dart on Windows in debug mode...
package:bug_test/main.dart:1
Connecting to VM Service at ws://127.0.0.1:63905/cNY-SYgW7KE=/ws
[GoRouter] known full paths for routes:
[GoRouter] => /
[GoRouter] => /profile
[GoRouter] => /setting
[GoRouter] setting initial location /
flutter: ══╡ EXCEPTION CAUGHT BY GOROUTER ╞══════════════════════════════════════════════════════════════════
flutter: The following _Exception was thrown Exception during GoRouter navigation:
flutter: Exception: 'package:go_router/src/go_router_delegate.dart': Failed assertion: line 299 pos 13:
package:go_router/src/go_router_delegate.dart:299
flutter: '!redirects.contains(redir)': redirect loop detected: / => /
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #2 GoRouterDelegate._getLocRouteMatchesWithRedirects.redirected
package:go_router/src/go_router_delegate.dart:299
flutter: #3 GoRouterDelegate._getLocRouteMatchesWithRedirects
package:go_router/src/go_router_delegate.dart:322
flutter: #4 GoRouterDelegate._go
package:go_router/src/go_router_delegate.dart:245
flutter: #5 new GoRouterDelegate
package:go_router/src/go_router_delegate.dart:58
flutter: #6 new GoRouter
package:go_router/src/go_router.dart:46
flutter: #7 FooRoute.routeTo
package:bug_test/main.dart:122
flutter: #8 new FooApp
package:bug_test/main.dart:169
flutter: #9 main
flutter: #10 _runMain.<anonymous closure> (dart:ui/hooks.dart:132:23)
flutter: #11 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
flutter: (elided 3 frames from class _AssertionError and class _RawReceivePortImpl)
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: Another exception was thrown: Exception: 'package:go_router/src/go_router_delegate.dart': Failed assertion: line 299 pos 13: '!redirects.contains(redir)': redirect loop detected: / => /
package:go_router/src/go_router_delegate.dart:299
[GoRouter] MaterialApp found
[GoRouter] refreshing /
2
flutter: Another exception was thrown: Exception: 'package:go_router/src/go_router_delegate.dart': Failed assertion: line 299 pos 13: '!redirects.contains(redir)': redirect loop detected: / => /
package:go_router/src/go_router_delegate.dart:299
[GoRouter] refreshing /
flutter: Another exception was thrown: Exception: 'package:go_router/src/go_router_delegate.dart': Failed assertion: line 299 pos 13: '!redirects.contains(redir)': redirect loop detected: / => /profile => /profile
package:go_router/src/go_router_delegate.dart:299
[GoRouter] refreshing /
[GoRouter] redirecting to /profile
flutter: Another exception was thrown: Exception: 'package:go_router/src/go_router_delegate.dart': Failed assertion: line 299 pos 13: '!redirects.contains(redir)': redirect loop detected: / => /profile => /profile
package:go_router/src/go_router_delegate.dart:299
[GoRouter] refreshing /
flutter: Another exception was thrown: Exception: 'package:go_router/src/go_router_delegate.dart': Failed assertion: line 299 pos 13: '!redirects.contains(redir)': redirect loop detected: / => /settings => /settings
package:go_router/src/go_router_delegate.dart:299
[GoRouter] refreshing /
[GoRouter] redirecting to /settings
Application finished.
Exited (sigterm)
如果有人可以幫助我,我將不勝感激。謝謝
uj5u.com熱心網友回復:
我想你誤解了redirect
GoRouter 的引數。
這個屬性監聽路由的每一個變化,并且是為了減輕某些情況下的重定向(例如,用戶斷開連接,所以你將他重定向到 /login 頁面)。
官方檔案解釋的很好。
洗掉此引數可以解決您的問題。
GoRouter routeTo() {
return GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const MyHomePage(),
),
GoRoute(
path: '/profile',
builder: (context, state) => const ProfilePage(),
),
GoRoute(
path: '/setting',
builder: (context, state) => const SettingPage(),
),
],
refreshListenable: notifier,
debugLogDiagnostics: true,
);
}
而不是使用這種方法來創建你的導航,你應該使用這里GoRouter.of(context).go('/profile')
解釋
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/480534.html
上一篇:SafeArea AppBar/NestedScrollView在Flutter中創建不需要的bar
下一篇:找到太多引數,但預期為0