繁体   English   中英

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

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

我正在从第一个屏幕导航到第二个屏幕。 我在第二个屏幕中使用 Bloc Pattern。 我收到以下错误:

 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.

下面是我的导航代码

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

下面是我的 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]);
        },
      ),
    );
  },
  );
  }
 }

我刚刚开始学习集团并卡在这里..有人可以帮忙吗?

我没有在 main.dart 中添加任何与 bloc 相关的内容。 有必要吗?

问题出在这部分:

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

将其更改为此可能会修复它:

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

将 Used widget包装在BlocProvider ,这将提供要使用的正确上下文。

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

要在您正在导航的页面中使用相同的块,请使用 BlocProvider.value()。

改变

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

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

确保有一个 MyBloc 类型的父 BlocProvider。 简单示例:

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

我还强烈建议您查看此视频以获得更好的解释。

此外,这个问题可能会有所帮助。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM