1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] How to access state of another bloc in Flutter bloc

Discussão em 'Mobile' iniciado por Stack, Outubro 3, 2024 às 10:22.

  1. Stack

    Stack Membro Participativo

    I'm relatively new to flutter bloc state management, so here is the scenario where I would be needing to access state of another bloc

    there are two bloc in my flutter app, the first one is UserBloc which stores user session id or token in its state (UserState) and the second one is MediaBloc, this bloc requires user session id or token to fetch the logged in user media list

    User Bloc

    class UserBloc extends Bloc<UserState, UserEvent>{

    }

    class UserState{
    String token;
    UserState({this.token});
    }


    Media Bloc

    class MediaBloc extends Bloc<MediaState, MediaEvent>{
    on<FetchMediaEvent>((event, state) async {
    List<Media> mediaList = await fetchUserMedia(token: ''); // how to access value of token which is in UserBloc state
    });
    }

    class MediaState{
    List<Media> mediaList;
    MediaState({this.mediaList});
    }


    class FetchMediaEvent {
    }

    Continue reading...

Compartilhe esta Página