BLoC 的事件映射器未执行,但调试建议它应该 8-/
event mapper of BLoC not executed, though debugging suggests it should 8-/
这很奇怪,也许你们中有人知道会发生什么:
我的集团有一些事件,以下事件导致了问题(其他尚未实施):
@freezed
abstract class ProblemManagerEvent with _$ProblemManagerEvent {
const factory ProblemManagerEvent.pmDataSyncRequested() = PmDataSyncRequested;
}
这应该会导致 bloc 发出一个状态
@injectable
class ProblemManagerBloc extends Bloc<ProblemManagerEvent, ProblemManagerState> {
final IJournalsRepository journalsRepository;
final IProblemsRepository problemsRepository;
ProblemManagerBloc(this.journalsRepository, this.problemsRepository) : super(PmInitial());
@override
Stream<ProblemManagerState> mapEventToState(
ProblemManagerEvent event,
) async* {
print('before map $event'); // <<<<<<<<<<<<<<<<<<<<<<<<
event.map(
pmDataSyncRequested: (e) async* {
print('in map $e'); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<
final Either<StorageFailure, Unit> response = await problemsRepository.syncProblems();
response.fold (
(f) async* {yield PmSyncFailed(f);},
(s) async* {yield PmInitial();});
print('yep');
yield ProblemManagerState.initial();
},
pmClassProblemRequested: (e) async* {
yield ProblemManagerState.initial(); //todo
},
pmRandomProblemRequested: (e) async* {
yield ProblemManagerState.initial(); //todo
}
);
print('post map'); // <<<<<<<<<<<<<<<<
}
}
这会产生以下输出
Restarted application in 4.549ms.
flutter: before map ProblemManagerEvent.pmDataSyncRequested()
flutter: post map
flutter: before map ProblemManagerEvent.pmDataSyncRequested()
flutter: post map
因此,即使它打印了正确的事件,mapEventToState 也不会触发与该事件关联的代码。尽管如此,它还是完成了 bloc 的代码,并出于某种原因再次运行该事件(一些出于某种原因进行了重建)。事件是这样触发的,下面的widgets都渲染在了屏幕上。
class ProblemPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final StackRouter router = context.router;
return PlatformScaffold(
appBar: PlatformAppBar(title: const Text('Home')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
BlocBuilder<ProblemManagerBloc, ProblemManagerState>(
builder: (context, state) {
context.read<ProblemManagerBloc>().add(PmDataSyncRequested());
return Column (children:[
Text('Hello world!'),
TextButton(child:Text('los'),
onPressed: () {print('home');
在 event.map(...)
之前缺少 yield*
。现在,它基本上与 void
函数没有 return/yield 任何东西相同。
这很奇怪,也许你们中有人知道会发生什么:
我的集团有一些事件,以下事件导致了问题(其他尚未实施):
@freezed
abstract class ProblemManagerEvent with _$ProblemManagerEvent {
const factory ProblemManagerEvent.pmDataSyncRequested() = PmDataSyncRequested;
}
这应该会导致 bloc 发出一个状态
@injectable
class ProblemManagerBloc extends Bloc<ProblemManagerEvent, ProblemManagerState> {
final IJournalsRepository journalsRepository;
final IProblemsRepository problemsRepository;
ProblemManagerBloc(this.journalsRepository, this.problemsRepository) : super(PmInitial());
@override
Stream<ProblemManagerState> mapEventToState(
ProblemManagerEvent event,
) async* {
print('before map $event'); // <<<<<<<<<<<<<<<<<<<<<<<<
event.map(
pmDataSyncRequested: (e) async* {
print('in map $e'); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<
final Either<StorageFailure, Unit> response = await problemsRepository.syncProblems();
response.fold (
(f) async* {yield PmSyncFailed(f);},
(s) async* {yield PmInitial();});
print('yep');
yield ProblemManagerState.initial();
},
pmClassProblemRequested: (e) async* {
yield ProblemManagerState.initial(); //todo
},
pmRandomProblemRequested: (e) async* {
yield ProblemManagerState.initial(); //todo
}
);
print('post map'); // <<<<<<<<<<<<<<<<
}
}
这会产生以下输出
Restarted application in 4.549ms.
flutter: before map ProblemManagerEvent.pmDataSyncRequested()
flutter: post map
flutter: before map ProblemManagerEvent.pmDataSyncRequested()
flutter: post map
因此,即使它打印了正确的事件,mapEventToState 也不会触发与该事件关联的代码。尽管如此,它还是完成了 bloc 的代码,并出于某种原因再次运行该事件(一些出于某种原因进行了重建)。事件是这样触发的,下面的widgets都渲染在了屏幕上。
class ProblemPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final StackRouter router = context.router;
return PlatformScaffold(
appBar: PlatformAppBar(title: const Text('Home')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
BlocBuilder<ProblemManagerBloc, ProblemManagerState>(
builder: (context, state) {
context.read<ProblemManagerBloc>().add(PmDataSyncRequested());
return Column (children:[
Text('Hello world!'),
TextButton(child:Text('los'),
onPressed: () {print('home');
event.map(...)
之前缺少 yield*
。现在,它基本上与 void
函数没有 return/yield 任何东西相同。