简体   繁体   English

BlocProvider 在继承的小部件中不可用

[英]BlocProvider not available within inherited widget

Facing an issue with BlocProviders.面临 BlocProviders 的问题。

As I understand, a bloc should be accessible anywhere within the scope of inherited widgets.据我了解,在继承的小部件范围内的任何地方都应该可以访问一个块。

I have an App class, which happens to be my core class, where I build my material App我有一个 App 类,它恰好是我的核心类,在那里我构建了我的材料 App

 Widget build(BuildContext context) {
  return MaterialApp(
    debugShowCheckedModeBanner: false,
    title: 'E-Form',
    color: Color(0xFF44697D),
    home: BlocProvider(child: LaunchScreen()),
  );
}

Now I have the LaunchScreen class, here I am not using my BlocProvider, but I have wrapped LaunchScreen altogether in my App class with BlocProvider现在我有了 LaunchScreen 类,在这里我没有使用我的 BlocProvider,但是我已经使用 BlocProvider 将 LaunchScreen 完全包装在我的 App 类中

class  LaunchScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final bloc = BlocProvider.of(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('First Screen'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Launch screen'),
          onPressed: () {
            // Navigate to second screen when tapped!
            Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => Home()),
            );
          },
        ),
      ),
    );
  }
}

Now again in my third class, that is Home() class.现在再次进入我的第三个类,即 Home() 类。 I am trying to use my BlocProvider.我正在尝试使用我的 BlocProvider。 Here I get an error saying the bloc is null在这里,我收到一条错误消息,指出该集团为空

class Home extends StatelessWidget {
  Widget build(context) {
    final bloc = BlocProvider.of(context);
    return  Scaffold(
      appBar: AppBar(
        title: Text('Reduced Course Load'),
        backgroundColor: Color(0xFF44697D),
      ),
      body: CustomWidget(),
    );
  }
}

I am unable to figure out why this is null.我无法弄清楚为什么这是空的。 The bloc is initialized perfectly if I initialize and print the bloc in LaunchScreen class.如果我在 LaunchScreen 类中初始化并打印该集团,该集团将被完美初始化。 Does BlocProvider work only within a single class scope? BlocProvider 是否仅在单个类范围内工作?

There's another error, I think my Navigator isn't working properly.还有一个错误,我认为我的导航器工作不正常。 For some reason, my Home() class constructor is getting recalled again.出于某种原因,我的 Home() 类构造函数再次被召回。 I am unable to fix the error in Navigator.我无法修复 Navigator 中的错误。 An example would be great for me to understand a navigator.一个例子对我理解导航器很有帮助。

Here is my flutter doctor results这是我的颤振医生结果

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, v0.8.2, on Mac OS X 10.13.6 17G65, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.0)
[✓] iOS toolchain - develop for iOS devices (Xcode 9.4.1)
[✓] Android Studio (version 3.2)
[✓] VS Code (version 1.27.2)
[✓] Connected devices (1 available)

Please take a look, and share a solution.请看一下,并分享一个解决方案。 Thank you!谢谢!

You should use your BlocProvider() above the MaterialApp widget.您应该在MaterialApp小部件上方使用您的BlocProvider()

Navigator is a widget added by MaterialApp and when you call it to change a route it replaces the widget in home: property and hence replaces your BlocProvider . NavigatorMaterialApp添加的小部件,当您调用它来更改路由时,它会替换home:属性中的小部件,从而替换您的BlocProvider

 Widget build(BuildContext context) {
  return BlocProvider(
    child: MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'E-Form',
      color: Color(0xFF44697D),
      home: LaunchScreen(),
    ),
  );
 }

Also you could pass your bloc to screen "Home" and there wrap your Scaffold in BlocProvider.value(yourBlocInstance).您也可以将您的集团传递到屏幕“主页”,然后将您的脚手架包装在 BlocProvider.value(yourBlocInstance) 中。 BTW, this lets you put initial providing your bloc with BlocProvider(create: (context) => YourBloc()) under MaterialApp and navigation would not break it.顺便说一句,这使您可以在 MaterialApp 下使用 BlocProvider(create: (context) => YourBloc()) 初始提供您的集团,导航不会破坏它。

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

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