Bloc: listen to state out of build

Created on 7 Feb 2019  路  16Comments  路  Source: felangel/bloc

i have a question

i want listen to state, and doing something with new state
for example i want listen to AuthenticationState, when is AuthenticationAuthenticated i want after 3 seconds push to new page, how can achieve to this ?

thanks

question

All 16 comments

Hello! 馃憢

You can do bloc.state.listen to react to state changes outside of build.

Does that help answer your question?

@felangel oh thanks,
just another question
i use dio to http requests, is possible use interceptor to catch 403 errors, if occurred, dispatch LoggedOut event to app that push my route to login route automatically ?

(sorry for my bad english, I hope you understand my conversation(

Yeah you can definitely do that! You鈥檒l just need to inject the bloc into your http interceptor and dispatch the event (like you said). If you have a code snippet that isn鈥檛 working I鈥檇 be happy to take a look but you have the right idea 馃憤

i think because i don't have context in interceptor, i can't inject AuthenticationBloc

You can directly inject the bloc via the constructor. You鈥檙e right that you can鈥檛 use BlocProvider without a BuildContext.

@felangel
thanks for consultation
thanks for your great library

and i have another question about this idea out of this library
is possible push login route and stop user to back page ?
i know i can hide back icon from appbar, but android users can using back key of phone

i found that

WillPopScope(
   onWillPop: () {
     SystemNavigator.pop();
   },
   child: ...
);

thanks

@felangel i faced with a problem

where does i must listen to authentication state ?
i have something :

authenticationBloc.state.listen((state) {
    if(state is AuthenticationUnauthenticated) {
      Navigator.pushNamedAndRemoveUntil(context, '/login', ((Route<dynamic> route) => false));
    }
});

but when state is AuthenticationUnauthenticated, i give this error :

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.

i tested it inside BlocProvider but any success

You need to do that within your MaterialApp widget. I would try moving that logic to the onInit override in your primary app component under MaterialApp. If you're able to share a link to your repo, I can also take a look and try to help.

sorry my application not exist in git

but i use this pattern for making my app

Out of curiosity have you looked at the Login Flow example here?

yeah, but my design is different
in my splash page, i have 2 button
login with email
login with phone number

if token set, buttons hides, and waiting for fetch user from api, when fetch successful i pushed route to /home

that's very similar except in your example you are pushing a new route as opposed to in the example I sent we use BlocBuilder and rebuild based on the AuthenticationState. Is there a reason you don't take a similar approach instead of pushing a route?

on of biggest problem is animating 馃榿
for example changing home page with login page is not smooth

Ah okay. Have you checked this out? https://github.com/felangel/bloc/issues/18

@felangel i will look into
big thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

1AlexFix1 picture 1AlexFix1  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments

abinvp picture abinvp  路  3Comments

zjjt picture zjjt  路  3Comments

timtraversy picture timtraversy  路  3Comments