以下是我的BLocProvider(在MainApp内):
final keyBlocProvider =
GlobalKey<NavigatorState>(debugLabel: "BlocProvider Navigator");
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
key: keyBlocProvider, // I'm trying to access BlocProvider with this key
providers: [
BlocProvider<ClockBloc>(
create: (_) => ClockBloc(),
),
],
child: MaterialApp(home: const App(),)
)
}
}
在MainApp
内部,我有一个名为App
的小部件,我try 在其中访问BlocProvider outside of my build function:
class _AppState extends State<App> {
@override
Widget build(BuildContext context) {}
/// Process notification when loading the app
void onNotificationListener(String? payload) {
var context = Navigator.of(keyBlocProvider.currentContext!).context; // my app freezes here
}
}
我这样做的原因是,当我的应用程序在用户按下通知时启动时,我想导航到某个页面,但要做到这一点,我首先必须调用某个Bloc事件.