我现在遇到了一个问题,那就是在我的Flutter 翼应用程序中无法访问Navigator
.代码是这样的:
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<LanguageBloc>(
create: (_) => serviceLocator<LanguageBloc>(),
),
BlocProvider<MaintenanceBloc>(
create: (_) => serviceLocator<MaintenanceBloc>(),
),
BlocProvider<VersionBloc>(
create: (_) => serviceLocator<VersionBloc>(),
),
BlocProvider<AuthBloc>(
create: (_) => serviceLocator<AuthBloc>(),
),
],
child: const _AppView(),
);
}
}
class _AppView extends StatefulWidget {
const _AppView();
@override
State<_AppView> createState() => _AppViewState();
}
class _AppViewState extends State<_AppView> {
final _navigatorKey = GlobalKey<NavigatorState>();
NavigatorState get _navigator => _navigatorKey.currentState!;
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: _navigatorKey,
title: AppConfig.appName,
locale: context.select((LanguageBloc bloc) => bloc.state.language.toLocale()),
supportedLocales: AppLanguage.toLocales(),
localizationsDelegates: [
AppLocalization.getDelegate(getAppTranslations: serviceLocator<GetAppTranslations>()),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
theme: AppTheme.themeData,
onGenerateRoute: (settings) {
return MaterialPageRoute(
settings: settings,
builder: (context) {
return const Placeholder(
color: Colors.red,
);
},
);
},
builder: (context, child) {
return MultiBlocListener(
listeners: [
BlocListener<MaintenanceBloc, MaintenanceState>(
listener: (context, state) {
switch (state.runtimeType) {
case InMaintenance:
_navigator.pushAndRemoveUntil<void>(
MaterialPageRoute(builder: (context) => const MaintenancePage()),
(route) => false,
);
break;
case NotInMaintenance:
break;
}
},
),
BlocListener<VersionBloc, VersionState>(
listener: (context, state) {
switch (state.runtimeType) {
case InitialVersionState:
break;
case CheckingVersion:
break;
case Outdated:
_navigator.pushAndRemoveUntil<void>(
MaterialPageRoute(builder: (context) => const OutdatedPage()),
(route) => false,
);
break;
case UpToDate:
break;
}
},
),
BlocListener<AuthBloc, AuthState>(
listener: (context, state) {
switch (state.status) {
case AuthStatus.authenticated:
_navigator.pushAndRemoveUntil<void>(
MaterialPageRoute(builder: (context) => MainPage()),
(route) => false,
);
case AuthStatus.unauthenticated:
_navigator.pushAndRemoveUntil<void>(
MaterialPageRoute(builder: (context) => const LoginPage()),
(route) => false,
);
case AuthStatus.unknown:
_navigator.pushAndRemoveUntil<void>(
MaterialPageRoute(builder: (context) => const LoginPage()),
(route) => false,
);
}
},
),
],
child: child!,
);
},
);
}
}
使用我的代码,我基本上遵循了您在BLoC Login Tutorial中看到的代码.
无论出于什么原因,这都会导致Unhandled Exception: Null check operator used on a null value
在NavigatorState get _navigator => _navigatorKey.currentState!;
,这意味着Navigator
不能被访问.
当我运行相同的代码,但没有使用_navigatorKey
,而是使用Navigator.of(context)
时,我收到以下错误:
Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.
个
The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
个
再次表明我不能使用Navigator
.
这一点让我感到困惑,因为MaterialApp
是一个parent
小部件,所以Navigator
应该是可访问的.将MultiBlocListener
包装在Builder
中以获得最新的context
并不能解决问题.不使用MultiBlocListener
内部的独立BlocListener
提供的context
也没有帮助.
在这个阶段,我不知道这里出了什么问题,特别是因为我正在遵循上面链接的教程.