英文:
MultiBlocProvider does not add event
问题
我将为您翻译代码部分,如下所示:
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => AppBloc()..add(AppStartedEvent()),
lazy: false,
child: BlocListener<AppBloc, AppState>(
listener: (context, state) {
if (state is AppStarted) {
_initializeDatabase();
}
},
),
),
BlocProvider(
create: (context) => GroupOverviewBloc()..add(LoadPlayers()),
lazy: false,
child: BlocListener<GroupOverviewBloc, PlayerOverviewState>(
listener: (context, state) {
if (state is GroupAdded || state is GroupDeleted) {
context.read<GroupOverviewBloc>().add(LoadPlayers());
}
},
),
),
BlocProvider(
create: (context) => PlayerFormCubit(),
),
BlocProvider(
create: (context) => HealthCubit(),
),
BlocProvider(
create: (context) => ManaCubit(),
),
],
child: const MaterialApp(
home: PlayerOverview(),
),
);
}
Future<void> _initializeDatabase() async {
await DBHelper.instance.database;
}
}
class AppBloc extends Bloc<AppEvent, AppState> {
AppBloc() : super(AppInitial()) {
on<AppStartedEvent>((event, emit) {
emit(AppStarted());
});
}
}
abstract class AppState {}
class AppInitial extends AppState {}
class AppStarted extends AppState {}
abstract class AppEvent {}
class AppStartedEvent extends AppEvent {}
希望这能帮助您理解代码。如果您有任何其他问题,请随时提出。
英文:
I had all my BlocProviders nested what worked but didn't look very good, so i rewrote it to a MultiBlocProvider as seen in the Code below.
The problem now is, that the event and with that the _initialiazeDatabase() function in the listener doesn't get called.
I already saw some posts where i need to set lazy to false which i did, but still not work.
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => AppBloc()..add(AppStartedEvent()),
lazy: false,
child: BlocListener<AppBloc, AppState>(
listener: (context, state) {
if (state is AppStarted) {
_initializeDatabase();
}
},
),
),
BlocProvider(
create: (context) => GroupOverviewBloc()..add(LoadPlayers()),
lazy: false,
child: BlocListener<GroupOverviewBloc, PlayerOverviewState>(
listener: (context, state) {
if (state is GroupAdded || state is GroupDeleted) {
context.read<GroupOverviewBloc>().add(LoadPlayers());
}
},
),
),
BlocProvider(
create: (context) => PlayerFormCubit(),
),
BlocProvider(
create: (context) => HealthCubit(),
),
BlocProvider(
create: (context) => ManaCubit(),
),
],
child: const MaterialApp(
home: PlayerOverview(),
),
);
}
Future<void> _initializeDatabase() async {
await DBHelper.instance.database;
}
}
class AppBloc extends Bloc<AppEvent, AppState> {
AppBloc() : super(AppInitial()) {
on<AppStartedEvent>((event, emit) {
emit(AppStarted());
});
}
}
abstract class AppState {}
class AppInitial extends AppState {}
class AppStarted extends AppState {}
abstract class AppEvent {}
class AppStartedEvent extends AppEvent {}
答案1
得分: 0
可以通过将MaterialApp包装在MultiBlocListener中,并将监听器放在其中来解决这个问题。
似乎Listeners在MultiBlocProvider中不起作用,现在我想起来这是有道理的。
MultiBlocProvider(
providers: [
// 我的所有Providers
],
child: MultiBlocListener(
listeners: [
// 我的所有Listeners
],
child: const MaterialApp(
home: PlayerOverview(),
),
),
);
英文:
Could fix it by wrapping the MaterialApp with a MultiBloCListener and putting the listeners in there.
Seems like the Listeners doesn't work in the MultiBlocProvider what makes sense now when i think of it.
MultiBlocProvider(
providers: [
// All my Providers
],
child: MultiBlocListener(
listeners: [
// All my Listeners
],
child: const MaterialApp(
home: PlayerOverview(),
),
),
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论