Reputation: 1618
I'm trying to implement a player using a BLOC pattern. When I change the state in the mapEventToState() method the BlocBulder() widget is not updating as expected.
This is the PlayerBloc:-
class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
@override
get initialState => PlayerState.Initialized;
@override
Stream<PlayerState> mapEventToState(PlayerEvent event) async* {
if (event is InitialLoad) {
yield PlayerState.Initialized;
} else if (event is PlayStation) {
yield PlayerState.Playing;
} else if (event is StationPlaying) {
yield PlayerState.Playing;
} else if (event is StationStopped) {
yield PlayerState.Stopped;
} else {
yield PlayerState.Stopped;
}
}
}
This is the BlocBuilder:-
class PlayerCollapsed extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<PlayerBloc, PlayerState>(
bloc: PlayerBloc(),
builder: (BuildContext context, PlayerState state) {
if (state == PlayerState.Initialized) {
print(state);
return Center(child: CircularProgressIndicator());
} else {
return Text("State Updated");
}
},
);
}
}
Any help will be appreciated. 😊
Upvotes: 0
Views: 954
Reputation: 2864
Okay what happened is you used an instance of PlayerBloc provided by BlocProvider to emit event but you use different instance of PlayerBloc for your BlocBuilder.
If you have provided PlayerBloc to the subtree with BlocProvider, you don't wanna make a new one.
class PlayerCollapsed extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<PlayerBloc, PlayerState>(
// bloc: PlayerBloc(), <= delete this part
builder: (BuildContext context, PlayerState state) {
if (state == PlayerState.Initialized) {
print(state);
return Center(child: CircularProgressIndicator());
} else {
return Text("State Updated");
}
},
);
}
}
You may want to check out this https://github.com/felangel/bloc/issues/943#issuecomment-596220570
Upvotes: 2