Pass Values through Stream / Bloc

Asked

Viewed 381 times

-1

I have a login_bloc.Dart file and within it I have the following situation.

final usuario = BehaviorSubject<String>();

 usuario.sink.add(user.email);

and in the file home_screen.Dart, I have the following situation.

final _loginBloc = LoginBlock();
Widget build(BuildContext context) {
    return StreamBuilder<String>(
      stream: _loginBloc.usuario,
      builder: (context, snapshot) {
        print(_loginBloc.usuario);
        print(snapshot.data);

What happens is that:

  • The print(_loginBloc.usuario) get back to me -> Instance of 'BehaviorSubject<String>'
  • The print(snapshot.data) get back to me -> null.

The question is, how do I bring my "user.email" to another item?

1 answer

0

You need to add a few more things to your Bloc, follow the modifications below...

In login_bloc.Dart

  final BehaviorSubject<String> _usuario = BehaviorSubject<String>.seeded("");
  Stream<String> get outUsuario  => _usuario .stream;
  Sink<String> get inUsuario  => _usuario .sink;

  inUsuario.add(user.email);

In home_screen.Dart

final _loginBloc = LoginBlock();
Widget build(BuildContext context) {
    return StreamBuilder<String>(
      stream: _loginBloc.outUsuario,
      builder: (context, snapshot) {
        if (!snapshot.hasData) /* Se não tiver nenhuma informação no BLoC retorna um Container vazio, senão executa o resto do código*/
          return Container()

        return Text(snapshot.data);
       });
 }

Explanation

The StreamBuilder as the name itself says need to receive in its parameter stream a Stream and you were passing to it the BehaviorSubject which serves, roughly speaking, as a controller...

With Bloc, most of the time, you will work on the properties Sink and Stream of their respective BehaviorSubject.

Observing

If the changes I suggested above do not work, you need to review the logic you are using to call your Bloc, because you are feeding the Bloc elsewhere, when making the call final _loginBloc = LoginBlock(); you will get a new Bloc, ie without the data you entered in the other place.

To access Bloc data that has been entered outside your class home_screen, you need to put your Blocprovider in your main Widgets tree this way

BlocProvider(
      blocs: [ 
        Bloc((i) => LoginBlock()), 
      ],
      child: PaginaInicial()
    ); 

And then to access it in your home_screen, instead

final _loginBloc = LoginBlock();

Do so

final _loginBloc = BlocProvider.getBloc<LoginBlock>();

You can take a look at my Github project Equipmentmaintenance, where I use Bloc.

  • Hello Matheus, I did as you suggested, but I realized that when I declare . seeded(" "); home_screen.Dart only receives the blank space. For example, if I declare. seeded("test"), even though inUsuario.add(user.email) is not receiving this parameter yet. Ai without the seeded, home_screen.Dart only gets null if you have any more suggestions. Anyway thank you very much, opened my mind, mainly with the explanation.

  • @Gatopreto a look again at my answer, I had forgotten to modify some things! The seeded("") it’s not necessary, you can do so BehaviorSubject<String>() only.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.