0

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

Daniel Roldán
  • 1,328
  • 3
  • 20
  • So the thing about bloc is that events trigger states, so what you see as a final result is a state which must have been triggered by an event, so what you need to do is to trigger that Event whenever you want to change state, and the only way would be to call the function that triggers that event, so that the event can present yo with a new state. – Denzel Apr 07 '22 at 13:36
  • @Denzel But attaching ..add(GetAInformation()) in the main.dart, did not already trigger the states ? because inside it for example is the state.loading and it is not executed. – Daniel Roldán Apr 07 '22 at 13:43
  • I think it's probably not triggering the function as expected, so the way I do this is to put the function in a place where I know it will be called in other to trigger the next state change, so say you're moving to a new screen the event would be triggered from the previous screen so when you get to the new screen you have a new state. – Denzel Apr 07 '22 at 13:48
  • @Denzel Ok, to finish, at what point would you then occupy the ..add(event()) in your BlocProvider if you can't update it afterwards. – Daniel Roldán Apr 07 '22 at 13:52
  • I think what that should hold is the class or repository that has to be passed to the blocs constructor. Here's an example https://github.com/Enigma-I-am/Clypto/blob/main/lib/presentation/details/cubit/detail_cubit.dart – Denzel Apr 07 '22 at 14:07
  • That's what I still don't understand, in that example it does the same as I have, it enters DetailScreen calling a function with states from the BlocProvider, if it exits and re-enters, it executes again, I don't understand why I don't. – Daniel Roldán Apr 07 '22 at 14:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243693/discussion-between-daniel-roldan-and-denzel). – Daniel Roldán Apr 07 '22 at 15:01

0 Answers0