如何在Bloc中重新触发初始事件而不是使用Initstate?

发布于 2025-01-20 04:29:39 字数 2421 浏览 1 评论 0原文

这就是事实,我将get_it用作依赖项注入,因此我有代码:

final sl = GetIt.instance;

Future<void> init() async {

  sl.registerFactory(
    () => ABloc(
      services: sl(),
    ),
  );

  [...]

}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();

    [...]
  }

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        [...]
        BlocProvider<ABloc>(
          create: (context) =>
              di.sl<ABloc>()..add(GetAInformation()), // trigger the event
        ),
      ],
      child: MaterialApp...
   );
  }
}

当我从第一个屏幕(screenlogin)输入到具有信息(screeninfo)的屏幕(screeninfo)时,输入到bloc并获取信息,但是当我返回我的屏幕(ScreenLogin)使用PuspNamedAndRemoveuntil模拟“注销”,然后再次返回不再进入集团并触发事件,我想知道我在做错了什么或如何重新触发我的工作方式再次活动。

class ScreenInfo extends StatelessWidget{


  // @override
  // void initState() {
  //   super.initState();
  //   BlocProvider.of<ABloc>(context).add(const GetAInformation());
  // }


  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () async {
          Navigator.of(context).pushNamedAndRemoveUntil(
            PageNames.screenLogin,
            (Route<dynamic> route) => false,
          },
          return false;
        },
        child: Scaffold(
          body: SafeArea(
            child: Padding(
              padding: const EdgeInsets.all(10),
              child: Column(
                children: <Widget>[
               
                  // BLOC
                  BlocBuilder<ABloc, AState>(
                    builder: (context, state) {
                      switch (state.status) {
                        case ProjectListStatus.success:
                          return Text(state);
                        case ProjectListStatus.failure:
                          return Text(state);

                        default:
                          return const LoadingWidget();
                      }
                    },
                  ),
                ],
              ),
            ),
          ),
    );
  }
}

PS:我曾经进入Screeninfo Apphreplacement amendem namplacement

Here's the thing, I use get_it as dependency injection, and so I have the code:

final sl = GetIt.instance;

Future<void> init() async {

  sl.registerFactory(
    () => ABloc(
      services: sl(),
    ),
  );

  [...]

}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();

    [...]
  }

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        [...]
        BlocProvider<ABloc>(
          create: (context) =>
              di.sl<ABloc>()..add(GetAInformation()), // trigger the event
        ),
      ],
      child: MaterialApp...
   );
  }
}

When I enter from the first screen (ScreenLogin), to my screen that has the information (ScreenInfo), enter to Bloc and gets the informacion, however when I return to my screen (ScreenLogin) simulating a "LogOut" with pushNamedAndRemoveUntil, and return again no longer enters to the Bloc and trigger the event, I would like to know what I'm doing wrong or how I do to re-trigger the event again.

class ScreenInfo extends StatelessWidget{


  // @override
  // void initState() {
  //   super.initState();
  //   BlocProvider.of<ABloc>(context).add(const GetAInformation());
  // }


  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () async {
          Navigator.of(context).pushNamedAndRemoveUntil(
            PageNames.screenLogin,
            (Route<dynamic> route) => false,
          },
          return false;
        },
        child: Scaffold(
          body: SafeArea(
            child: Padding(
              padding: const EdgeInsets.all(10),
              child: Column(
                children: <Widget>[
               
                  // BLOC
                  BlocBuilder<ABloc, AState>(
                    builder: (context, state) {
                      switch (state.status) {
                        case ProjectListStatus.success:
                          return Text(state);
                        case ProjectListStatus.failure:
                          return Text(state);

                        default:
                          return const LoadingWidget();
                      }
                    },
                  ),
                ],
              ),
            ),
          ),
    );
  }
}

PS : I use to enter ScreenInfo a pushReplacementNamed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文