在 ListView.builder Bloc 事件只触发一次

In ListView.builder Bloc event triggered only once

我正在使用 Bloc 进行状态管理,我在屏幕上调用 事件 ListView.builder

   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);
            },
          ),
        );
  },
);
 }
   }

经过 2 天的努力,我找到了这个问题的解决方案,我必须用另一个 BlocProvider 包装我的容器并使用 dependency injection

  loadSuccess: (state) {
        return ListView.builder(
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          itemCount: state.questions.size,
          itemBuilder: (
            context,
            index,
          ) {
            // ignore: avoid_unnecessar_containers

            return BlocProvider(
              create: (context) => getIt<UsersWatcherBloc>()
                ..add(
                  UsersWatcherEvent.watchCurrentUser(
                    state.questions.get(index).userId.getorCrash(),
                  ),
                ),
              child: Container(
                color: Colors.white,
                margin: const EdgeInsets.only(bottom: 5),
                padding: EdgeInsets.only(
                  left: leftPadding.w - 8.w,
                  right: rightpadding.w - 8.w,
                  bottom: bottomPadding.h,
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [