ProviderNotFoundException in flutter app with Bloc

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

ProviderNotFoundException in flutter app with Bloc

问题

以下是您要翻译的内容:

"I want to create an app containing a few screens where a user have a possibility to choose different options. On the last screen I have a checking if the object already exists in the database with bloc. However, I am constantly getting this error:
screenshot of the error.
It only happens with this part of the app, in the others everything works perfectly. I navigate between screens by Navigator.pushNamed and app router, which is initialized inside main file. This is the main file itself:"

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await di.init();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    DateTime max_date =
        DateTime(DateTime.now().year, (DateTime.now().month + 1) % 13, 0);
    return MultiBlocProvider(
      providers: [
        BlocProvider<MoodBloc>(
            create: (context) => di.sl<MoodBloc>()
              ..add(LoadMoodsEvent(
                  max_date: max_date, days_amount: max_date.day))),
      ],
      child: MaterialApp(
        theme: theme(),
        debugShowCheckedModeBanner: false,
        //home: ChooseMoodPage(),
        onGenerateRoute: AppRouter.onGenerateRoute,
        initialRoute: HomePage.routeName,
      ),
    );
  }
}

This is the part of code, which causes the error:

Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          BlocListener(
            listener: (context, state) {
              if (state is MoodStoreLoaded) {
                BlocProvider.of<MoodBloc>(context).add(
                    LoadMoodsEvent(max_date: DateTime.now(), days_amount: 30));
                Navigator.pushReplacementNamed(context, '/');
              } else if (state is MoodDeletedByDate) {
                BlocProvider.of<MoodBloc>(context).add(
                  StoreMoodEvent(
                    mood: MoodEntity(
                      username: 'user1',
                      date: widget.args.max_date!,
                      value: widget.args.mood!,
                      sphere: widget.args.sphere,
                      heading: widget.args.heading,
                      note: widget.args.note,
                      photos: convert_photo_list_to_string(
                          widget.args.photos ?? []),
                    ),
                  ),
                );
              }
            },

It's written everywhere, that the problem is about the parent widgets and provider relation, but I have initialized everything inside the head parent of my app."

英文:

I want to create an app containing a few screens where a user have a possibility to choose different options. On the last screen I have a checking if the object already exists in the database with bloc. However, I am constantly getting this error:
screenshot of the error.
It only happens with this part of the app, in the others everything works perfectly. I navigate between screens by Navigator.pushNamed and app router, which is initialized inside main file. This is the main file itself:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await di.init();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    DateTime max_date =
        DateTime(DateTime.now().year, (DateTime.now().month + 1) % 13, 0);
    return MultiBlocProvider(
      providers: [
        BlocProvider&lt;MoodBloc&gt;(
            create: (context) =&gt; di.sl&lt;MoodBloc&gt;()
              ..add(LoadMoodsEvent(
                  max_date: max_date, days_amount: max_date.day))),
      ],
      child: MaterialApp(
        theme: theme(),
        debugShowCheckedModeBanner: false,
        //home: ChooseMoodPage(),
        onGenerateRoute: AppRouter.onGenerateRoute,
        initialRoute: HomePage.routeName,
      ),
    );
  }
}

This is the part of code, which causes the error:

Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          BlocListener(
            listener: (context, state) {
              if (state is MoodStoreLoaded) {
                BlocProvider.of&lt;MoodBloc&gt;(context).add(
                    LoadMoodsEvent(max_date: DateTime.now(), days_amount: 30));
                Navigator.pushReplacementNamed(context, &#39;/&#39;);
              } else if (state is MoodDeletedByDate) {
                BlocProvider.of&lt;MoodBloc&gt;(context).add(
                  StoreMoodEvent(
                    mood: MoodEntity(
                      username: &#39;user1&#39;,
                      date: widget.args.max_date!,
                      value: widget.args.mood!,
                      sphere: widget.args.sphere,
                      heading: widget.args.heading,
                      note: widget.args.note,
                      photos: convert_photo_list_to_string(
                          widget.args.photos ?? []),
                    ),
                  ),
                );
              }
            },
  

It's written everywhere, that the problem is about the parent widgets and provider relation, but I have initialized everything inside the head parent of my app.

答案1

得分: 0

添加一个使用 <MoodBloc, MoodState>BlocListener,就像这样:BlocListener<MoodBloc, MoodState>

附言:我假设 MoodState 是你的 MoodBloc 的状态。

英文:

Add BlocListener with &lt;MoodBloc, MoodState&gt; like BlocListener&lt;MoodBloc, MoodState&gt;.

P.S. I supposed MoodState is the state of your MoodBloc

huangapple
  • 本文由 发表于 2023年2月26日 22:18:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572599.html
匿名

发表评论

匿名网友

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

确定