MultiBlocProvider不会添加事件。

huangapple go评论72阅读模式
英文:

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) =&gt; AppBloc()..add(AppStartedEvent()),
          lazy: false,
          child: BlocListener&lt;AppBloc, AppState&gt;(
            listener: (context, state) {
              if (state is AppStarted) {
                _initializeDatabase();
              }
            },
          ),
        ),
        BlocProvider(
          create: (context) =&gt; GroupOverviewBloc()..add(LoadPlayers()),
          lazy: false,
          child: BlocListener&lt;GroupOverviewBloc, PlayerOverviewState&gt;(
            listener: (context, state) {
              if (state is GroupAdded || state is GroupDeleted) {
                context.read&lt;GroupOverviewBloc&gt;().add(LoadPlayers());
              }
            },
          ),
        ),
        BlocProvider(
          create: (context) =&gt; PlayerFormCubit(),
        ),
        BlocProvider(
          create: (context) =&gt; HealthCubit(),
        ),
        BlocProvider(
          create: (context) =&gt; ManaCubit(),
        ),
      ],
      child: const MaterialApp(
        home: PlayerOverview(),
      ),
    );
  }

  Future&lt;void&gt; _initializeDatabase() async {
    await DBHelper.instance.database;
  }
}

class AppBloc extends Bloc&lt;AppEvent, AppState&gt; {
  AppBloc() : super(AppInitial()) {
    on&lt;AppStartedEvent&gt;((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(),
        ),
      ),
    );

huangapple
  • 本文由 发表于 2023年7月31日 20:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定