简体   繁体   English

使用不包含 Bloc 类型的上下文调用 Flutter BlocProvider.of()

[英]Flutter BlocProvider.of() called with a context that does not contain a Bloc of type

I am Navigating from first screen to second screen.我正在从第一个屏幕导航到第二个屏幕。 I am using Bloc Pattern in the second screen.我在第二个屏幕中使用 Bloc Pattern。 I am getting the below error:我收到以下错误:

 BlocProvider.of() called with a context that does not contain a Bloc of type 
   No ancestor could be found starting from the context that was passed to BlocProvider.of<MyBloc>().

    This can happen if:
    1. The context you used comes from a widget above the BlocProvider.
    2. You used MultiBlocProvider and didn't explicity provide the BlocProvider types.

Below is my code of navigation下面是我的导航代码

 BlocProvider<MyBloc>(
            create: (context) => MyBloc());
        Navigator.push(context, MaterialPageRoute(builder: (context) {
          
          return SecondScreen(context);
        }));

and below is my SecondScreen class下面是我的 SecondScreen 课程

  class SecondScreen extends StatefulWidget {
  @override
  _SecondScreenState createState() => _SecondScreenState();
   }

class _SecondScreenState extends State<SecondScreen> {
  MyBloc myBloc;

  @override
  void initState() {
    super.initState();
    myBloc = BlocProvider.of<MyBloc>(context);
    myBloc.add(FetchEvents());
 }

  @override
  Widget build(BuildContext context) {
  return MaterialApp(
  home: Builder(
    builder: (context) {
      return Material(
        child: Scaffold(
          appBar: AppBar(
            title: Text("New Screen"),
            actions: <Widget>[
              IconButton(
                icon: Icon(Icons.refresh),
                onPressed: () {
                  myBloc.add(FetchEvents());
                },
              ),
              IconButton(
                icon: Icon(Icons.info),
                onPressed: () {
                },
              )
            ],
          ),
          body: Container(
            child: BlocListener<MyBloc, MyState>(
              listener: (context, state) {
                if (state is MyErrorState) {
                  Scaffold.of(context).showSnackBar(
                    SnackBar(
                      content: Text(state.message),
                    ),
                  );
                }
              },
              child: BlocBuilder<MyBloc, MyState>(
                builder: (context, state) {
                  if (state is MyInitialState) {
                    return buildLoading();
                  } else if (state is MyLoadingState) {
                    return buildLoading();
                  } else if (state is MyLoadedState) {
                    return buildArticleList(state.yList);
                  } else if (state is MyErrorState) {
                    return buildErrorUi(state.message);
                  }
                },
              ),
            ),
          ),
        ),
      );
    },
  ),
  );
   }

 Widget buildLoading() {
  return Center(
  child: CircularProgressIndicator(),
   );
 }

  Widget buildErrorUi(String message) {
 return Center(
  child: Padding(
    padding: const EdgeInsets.all(8.0),
    child: Text(
      message,
      style: TextStyle(color: Colors.red),
    ),
  ),
  );
 }

  Widget buildArticleList(List<Data> data) {
  return ListView.builder(
  itemCount: data.length,
  itemBuilder: (ctx, pos) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: InkWell(
        child: ListTile(
         
          title: Text(data[pos].title),
          subtitle: Text(data[pos].shortDesc),
        ),
        onTap: () {
          // navigateToArticleDetailPage(context, articles[pos]);
        },
      ),
    );
  },
  );
  }
 }

I have just started learning bloc and stuck here.. Can anyone please help.我刚刚开始学习集团并卡在这里..有人可以帮忙吗?

I have not added anything related to bloc in main.dart.我没有在 main.dart 中添加任何与 bloc 相关的内容。 Is that needed?有必要吗?

The problem lies in this part:问题出在这部分:

 BlocProvider<MyBloc>(
            create: (context) => MyBloc());
        Navigator.push(context, MaterialPageRoute(builder: (context) {
          
          return SecondScreen(context);
        }));

Change it to this will presumably fix it:将其更改为此可能会修复它:

 BlocProvider<MyBloc>(
            create: (context) => MyBloc());
        Navigator.push(context, MaterialPageRoute(
          builder: (routeContext) => SecondScreen(),
        }));

Wrap Used widget inside BlocProvider which will provide the right context to be used.将 Used widget包装在BlocProvider ,这将提供要使用的正确上下文。

BlocProvider(
      create: (BuildContext context) {
          return _yourBloc;
      },
      child: Widget(

To use the same bloc in the page you are navigating to use BlocProvider.value().要在您正在导航的页面中使用相同的块,请使用 BlocProvider.value()。

Change改变

    Navigator.push(context, MaterialPageRoute(builder: (context) {  
        return SecondScreen(context);
    }));

to

Navigator.push(context, MaterialPageRoute(builder: (context) {
  return BlocProvider<MyBloc>.value(
    value: BlocProvider.of<MyBloc>(context),
    child: SecondScreen(context),
  );
}));

Make sure there is a parent BlocProvider of type MyBloc.确保有一个 MyBloc 类型的父 BlocProvider。 Simple Example:简单示例:

BlocProvider<MyBloc>(
  create: (context) => MyBloc(),
  child: Builder(
    builder: (context) => YourWidget(),//Wrap with Builder to access BlocProvider's context
  ),
);

I also highly recommend checking out this video for better explanation.我还强烈建议您查看此视频以获得更好的解释。

Also, this issue might help.此外,这个问题可能会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 使用不包含 Bloc 类型的上下文调用的 BLoC BlocProvider.of() - Flutter BLoC BlocProvider.of() called with a context that does not contain a Bloc of type Flutter:使用不包含 Bloc 类型的上下文调用 blocprovider.of() - Flutter: blocprovider.of() called with a context that does not contain a Bloc of type Flutter Bloc Cubit Navigator Problem BlocProvider.of() 调用的上下文不包含 CityCubit 类型的 Bloc/Cubit - Flutter Bloc Cubit Navigator Problem BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type CityCubit BlocProvider.of() 使用不包含 TrackingBloc 类型的 Bloc 的上下文调用 - BlocProvider.of() called with a context that does not contain a Bloc of type TrackingBloc 使用不包含 FicheMvtBloc 类型的 Bloc 的上下文调用 BlocProvider.of() - BlocProvider.of() called with a context that does not contain a Bloc of type FicheMvtBloc Flutter / BLoC - BlocProvider.of() 调用的上下文不包含 ArticlesBloc 类型的 Bloc - Flutter / BLoC - BlocProvider.of() called with a context that does not contain a Bloc of type ArticlesBloc BlocProvider.of() 使用不包含 OfflineBloc 类型的 Bloc 的上下文调用 - BlocProvider.of() called with a context that does not contain a Bloc of type OfflineBloc BlocProvider.of() 调用的上下文不包含 WeatherBloc 类型的 Bloc/Cubit - BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type WeatherBloc 使用不包含 Bloc 类型的上下文调用 BlocProvider.of() - BlocProvider.of() called with a context that does not contain a Bloc of type BlocProvider.of() 使用不包含 CLASS 类型的 Bloc 的上下文调用 - BlocProvider.of() called with a context that does not contain a Bloc of type CLASS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM