首页 > 解决方案 > 在启动画面中处理多个块并在主屏幕中显示结果

问题描述

我是 Flutter 的新手,我正在尝试在我的项目中实现 blocs。我需要帮助来实施多个集团。

情况如下:

main.dart:一旦应用程序启动,它将重定向到启动屏幕(使用自动路由包)

return MultiBlocProvider(
  providers: [
    BlocProvider(
      create: (context) =>
          getIt<AuthBloc>()..add(const AuthEvent.authCheckRequested()),
    ),
    BlocProvider<PostWatcherBloc>(
      create: (context) => getIt<PostWatcherBloc>()
        ..add(PostWatcherEvent.watchPost()),
    ),
    BlocProvider<FeaturedWatcherBloc>(
      create: (context) => getIt<FeaturedWatcherBloc>()
        ..add(FeaturedWatcherEvent.watchFeatured()),
    ),
    BlocProvider<CategoryWatcherBloc>(
      create: (context) => getIt<CategoryWatcherBloc>()
        ..add(CategoryWatcherEvent.watchCategories()),
    ),
   
  ],
  child: MaterialApp.router(
    title: Constants.appName,
    debugShowCheckedModeBanner: false,
    ),
    routerDelegate: _appRouter.delegate(),
    routeInformationParser: _appRouter.defaultRouteParser(),
  ),

splash_page.dart:

return MultiBlocListener(
  listeners: [
    BlocListener<AuthBloc, AuthState>(listener: (context, state) {
      state.map(
          initial: (_) {},
          authenticated: (_) => {
                print("Authenticated"),
                AutoRouter.of(context).push(HomePageRoute())
              },
          unauthenticated: (_) => {
                print("Unauthenticated"),
                AutoRouter.of(context).push(HomePageRoute())
              });
    }),
  ],
  child: const Scaffold(
    body: Center(
      child: CircularProgressIndicator(),
    ),
  ),
);

我的目标是一旦 Post/Category/FeaturedWatcherBloc 和 AuthBloc 被调用,它们的状态将被传递到 HomePage。基本上,一旦启动屏幕加载完成,主页上的帖子、精选和类别以及用户是否已登录都会准备好。

标签: flutterflutter-layoutflutter-webbloc

解决方案


推荐阅读