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.
– Dwizard
@Gatopreto a look again at my answer, I had forgotten to modify some things! The
seeded("")
it’s not necessary, you can do soBehaviorSubject<String>()
only.– Matheus Ribeiro