BlocBuilder 未根据状态更改进行更新
BlocBuilder is not updating on state change
我正在尝试使用 BLOC 模式实现播放器。当我在 mapEventToState() 方法中更改状态时,BlocBulder() 小部件未按预期更新。
这是 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;
}
}
}
这是 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");
}
},
);
}
}
如有任何帮助,我们将不胜感激。
好的,您使用了 BlocProvider 提供的 PlayerBloc 实例来发出事件,但您为 BlocBuilder 使用了不同的 PlayerBloc 实例。
如果您已经使用 BlocProvider 将 PlayerBloc 提供给子树,您不想创建一个新的。
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");
}
},
);
}
}
你可能想看看这个https://github.com/felangel/bloc/issues/943#issuecomment-596220570
我正在尝试使用 BLOC 模式实现播放器。当我在 mapEventToState() 方法中更改状态时,BlocBulder() 小部件未按预期更新。
这是 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;
}
}
}
这是 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");
}
},
);
}
}
如有任何帮助,我们将不胜感激。
好的,您使用了 BlocProvider 提供的 PlayerBloc 实例来发出事件,但您为 BlocBuilder 使用了不同的 PlayerBloc 实例。
如果您已经使用 BlocProvider 将 PlayerBloc 提供给子树,您不想创建一个新的。
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");
}
},
);
}
}
你可能想看看这个https://github.com/felangel/bloc/issues/943#issuecomment-596220570