简体   繁体   English

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

I use Bloc (Cubit) for data management in my Flutter project.我在 Flutter 项目中使用 Bloc (Cubit) 进行数据管理。 I get the following error in navigation operations.我在导航操作中收到以下错误。

BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type CityCubit. BlocProvider.of() 使用不包含 CityCubit 类型的 Bloc/Cubit 的上下文调用。 No ancestor could be found starting from the context that was passed to BlocProvider.of().从传递给 BlocProvider.of() 的上下文中找不到祖先。

When I go to my city list page from the login form, I get the above error.当我从登录表单 go 到我的城市列表页面时,出现上述错误。 The error goes away when I refresh the project.当我刷新项目时,错误消失了。 Cities are listed.列出了城市。 However, when I log in again and redirect me, I get an error.但是,当我再次登录并重定向我时,我收到一个错误。

main.dart --> MultiBlocProvider[providers: BlocProvider, BlocProvider] main.dart --> MultiBlocProvider[providers: BlocProvider, BlocProvider]

With the help of the button "BlocProvider.of (context).signIn (user);"借助按钮“BlocProvider.of (context).signIn (user);” logging in.在登录。

AutPage自动页面

BlocConsumer<AuthCubit, AuthState>(builder: (context, state) {
              return _buildUI();
            }, listener: (context, state) {
              if (state is AuthLoading) {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
              if (state is AuthLoaded) {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => CityListPage()));
              }
              if (state is AuthError) {
                Scaffold.of(context)
                    .showSnackBar(SnackBar(content: Text(state.message)));
                print(state.message);
              }
            })

CityListPage城市列表页

List<CityModel> cities = [];        
@override
          void initState() {
            BlocProvider.of<CityCubit>(context).getAll();
            super.initState();
          }
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text("Hello"),
            ),
            body: BlocConsumer<CityCubit, CityState>(builder: (context, state) {
              if (state is CityInitial) {
                return Text("....");
              }
              if (state is CityLoading) {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
              if (state is CitiesLoaded) {
                cities.addAll(state.cities);
              }
              return _buildCities();
            }, listener: (context, state) {
              if (state is CityError) {
                print("Error");
              }
            }));
      }
if (state is AuthLoaded) {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => CityListPage()));
              }

Here use BlocProvider or BlocProvider.value这里使用 BlocProvider 或 BlocProvider.value

Something Like Below,像下面这样的东西,

if (state is AuthLoaded) {
                    Navigator.of(context).push(
                        MaterialPageRoute(builder: (context) => BlocProvider(
            create: (context) => CityCubit(),
            child: CityListPage(),
          )));
                  }

暂无
暂无

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

相关问题 BlocProvider.of() 调用的上下文不包含 WeatherBloc 类型的 Bloc/Cubit - BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type WeatherBloc BlocProvider.of() 调用的上下文不包含 MyBloc 类型的 Bloc/Cubit - BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type MyBloc 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 使用不包含 Bloc 类型的上下文调用 Flutter BlocProvider.of() - Flutter BlocProvider.of() called with a context that does not contain a Bloc of type BlocProvider.of() 使用不包含 CounterBloc 类型的 Cubit 的上下文调用 - BlocProvider.of() called with a context that does not contain a Cubit of type CounterBloc BlocProvider.of() 使用不包含 Cubit 类型的上下文调用 - BlocProvider.of() called with a context that does not contain a Cubit of type 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() 使用不包含 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM