我想用GoRoute和Riverpod创建一个身份验证流。
如下所示:
1.显示启动画面。
1.当显示启动画面时,检查用户是否具有令牌。
1.基于该结果,将用户导航到登录屏幕或主屏幕。
当尝试运行应用程序时,发生了此错误:
════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building MaterialApp(dirty, state: _MaterialAppState#8a7c5):
If the routerConfig is provided, all the other router delegates must not be provided
'package:flutter/src/widgets/app.dart':
app.dart:1
Failed assertion: line 454 pos 14: '(routeInformationProvider ?? routeInformationParser ?? routerDelegate ?? backButtonDispatcher) == null'
AppRouting类:
class AppRouting {
// final appService;
final Ref ref;
GoRouter get router => _routes;
AppRouting({
required this.ref,
});
late final appService = ref.watch(appServiceProvider);
late final _routes = GoRouter(
refreshListenable: appService,
initialLocation: APP_PAGE.home.toPath,
routes: [
GoRoute(
name: APP_PAGE.home.toName,
path: APP_PAGE.home.toPath,
// builder: (context, state) => const HomeScreen(),
builder: (context, state) => const LoginScreen(),
),
GoRoute(
name: APP_PAGE.login.toName,
path: APP_PAGE.login.toPath,
builder: (context, state) => const LoginScreen(),
),
GoRoute(
name: APP_PAGE.resetPassword.toName,
path: APP_PAGE.resetPassword.toPath,
builder: (context, state) => const ResetPasswordScreen(),
),
GoRoute(
name: APP_PAGE.listView.toName,
path: APP_PAGE.listView.toPath,
builder: (context, state) => ListViewScreen(),
),
GoRoute(
name: APP_PAGE.palettQrCode.toName,
path: APP_PAGE.palettQrCode.toPath,
builder: (context, state) => PalettQrCode(),
),
GoRoute(
name: APP_PAGE.settings.toName,
path: APP_PAGE.settings.toPath,
builder: (context, state) => SettingScreen(),
),
// GoRoute(
// name: AppRoutingConstans.girdViewRouteName,
// path: GridViewScreen.screenPath,
// builder: (context, state) => GridViewScreen(),
// ),
GoRoute(
name: APP_PAGE.productPicker.toName,
path: APP_PAGE.productPicker.toPath,
builder: (context, state) => ProductPickUpScreen(),
),
GoRoute(
name: APP_PAGE.error.toName,
path: APP_PAGE.error.toPath,
builder: (context, state) => ErrorScreen(
error: state.extra.toString(),
),
),
GoRoute(
name: APP_PAGE.splash.toName,
path: APP_PAGE.splash.toPath,
builder: (context, state) => const SplashScreen(),
),
],
errorBuilder: (context, state) => ErrorScreen(
error: state.error.toString(),
),
redirect: (context, state) {
final splashLocation = state.namedLocation(APP_PAGE.splash.toPath);
final homeLocation = state.namedLocation(APP_PAGE.home.toPath);
final loginLocation = state.namedLocation(APP_PAGE.login.toPath);
final isLogedIn = appService.loginState;
final isInitialized = appService.initialized;
final isGoingToLogin = state.path == loginLocation;
final isGoingToInit = state.path == splashLocation;
print(state.path);
// If not Initialized and not going to Initialized redirect to Splash
if (!isInitialized && !isGoingToInit) {
return splashLocation;
// If not logedin and not going to login redirect to Login
} else if (isInitialized && !isLogedIn && !isGoingToLogin) {
return loginLocation;
// If all the scenarios are cleared but still going to any of that screen redirect to Home
} else if ((isLogedIn && isGoingToLogin) ||
(isInitialized && isGoingToInit)) {
return homeLocation;
} else {
// Else Don't do anything
return null;
}
},
);
}
// GoRouter configuration
final goRouterProvider = Provider<AppRouting>((ref) {
return AppRouting(ref: ref);
});
在主.dart
class MyApp extends ConsumerStatefulWidget {
const MyApp({super.key});
@override
ConsumerState<ConsumerStatefulWidget> createState() => _MyAppState();
}
class _MyAppState extends ConsumerState<MyApp> {
@override
Widget build(BuildContext context) {
final goRouter = ref.read(goRouterProvider).router;
return MaterialApp.router(
...
routerConfig: goRouter,
routeInformationProvider: goRouter.routeInformationProvider,
routeInformationParser: goRouter.routeInformationParser,
routerDelegate: goRouter.routerDelegate,
...
);
}
}
1条答案
按热度按时间omvjsjqw1#
从错误消息:
如果提供了
routerConfig
,则不能提供所有其他路由器委托。更改此:
对此: