【发布时间】:2021-11-24 01:35:50
【问题描述】:
我正在使用 Bloc 进行状态管理,我的屏幕在 ListView.builder 中调用 event
loadSuccess: (state) {
return ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: state.questions.size,
itemBuilder: (
context,
index,
) {
// ignore: avoid_unnecessary_containers
debugPrint("this is index $index");
debugPrint(
"this is user id ${state.questions.get(index).userId.getorCrash()}");
context.read<UsersWatcherBloc>().add(
UsersWatcherEvent.watchAllUsers(
state.questions.get(index).userId.getorCrash(),
),
);
return Container(
color: Colors.white,
......)
但问题是我的事件只触发了一次,状态改变了一次,但我想为每个索引更改我的事件:
Event.dart:
part of 'users_watcher_bloc.dart';
@freezed
abstract class UsersWatcherEvent with _$UsersWatcherEvent {
const factory UsersWatcherEvent.watchAllUsers(String uId) = _Started;
}
Bloc.dart:
@injectable
class UsersWatcherBloc extends Bloc<UsersWatcherEvent, UsersWatcherState> {
final IElearningRepository _iElearningRepository;
UsersWatcherBloc(this._iElearningRepository)
: super(const UsersWatcherState.initial());
@override
Stream<UsersWatcherState> mapEventToState(
UsersWatcherEvent event,
) async* {
yield* event.map(
watchAllUsers: (e) async* {
print("this is user id ${e.uId}");
yield const UsersWatcherState.loadInProgress();
yield* _iElearningRepository.watchAllUsers(e.uId.toString()).map(
(failureOrUsers) => failureOrUsers.fold(
(f) => UsersWatcherState.loadFailure(f),
(users) {
if (users.isEmpty) {
return const UsersWatcherState.empty();
}
return UsersWatcherState.loadSuccess(users);
},
),
);
},
);
}
}
【问题讨论】:
-
什么是
loadSuccess: (state) {?什么小部件调用loadSuccess回调? -
@pskink : Blocbuilder
-
Blocbuilder 没有它 - 它没有
loadSuccess属性 -
我有状态加载成功
-
我不明白你的意思 - 你为什么不简单地发布整个代码,它是
loadSuccess: (state) {的一部分?