简体   繁体   English

我如何使 bloc 可用于所有 flutter 应用程序页面

[英]how can i make bloc available for all flutter app pages

I am using bloc to manage my app state, I want to provide the bloc for all my app pages so I have inserted in the top of the widget tree so I can use it from any place in the widget tree, I have used it as the follows我正在使用 bloc 来管理我的应用程序 state,我想为我的所有应用程序页面提供 bloc,因此我已将其插入到小部件树的顶部,以便我可以从小部件树中的任何位置使用它,我已将其用作以下

1- main page 1- 主页

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyAppState();
  }}

  class MyAppState extends State<MyApp>{

  @override
  Widget build(BuildContext context) {
    return BlocProvider<MyBloc>(
      create: (BuildContext context) {
        return MyBloc();
      },
      child: MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: secondPage()),
    );
  }
}

2- secondPage: 2-秒页:

 class SecondPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return SecondPage State();
  }
}

class SecondPage State extends State<SecondPage > {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('secondPage')),

        body: BlocBuilder<CityBloc, CityState>(
          builder: (BuildContext context, CityState state) {            
             .......  
          },));}}

but the flutter display an error that但 flutter 显示错误

BlocProvider.of() called with a context that does not contain a Bloc of type MyBloc

and this is a screenshot of the app's widgets tree这是应用程序小部件树的屏幕截图小部件树屏幕截图

, what is the error, I want to provide mybloc for all widgets ,什么是错误,我想为所有小部件提供 mybloc

note: the app run ok if I write the MainPage class and the secondPage class in the same page, but when I separate them the error appears注意:如果我在同一页面中写入 MainPage class 和 secondPage class,应用程序运行正常,但是当我将它们分开时出现错误

I was shocked by the solution, the problem was only in import, I have replaced我对解决方案感到震惊,问题仅在导入中,我已更换

import '../blocs/blocs.dart';

With

import 'package: loony_trips / blocs / blocs.dart';

And everything was fixed, even though the two sentences were supposed to be the same一切都是固定的,即使这两个句子应该是一样的

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

相关问题 如何从一个可用于 Flutter 中的所有其他区块的区块中制作 object - How to make an object from a bloc available for all other bloc in Flutter 如何将变量从导航器设置为全局变量以在 flutter 应用程序的所有页面中可用? - How can I set a variable from navigator to global to be available in all pages in flutter app? 如何让应用栏停留在 flutter 中的所有页面上? - how to make the App bar stay across all the pages in flutter? 如何让 pageView.Builder 占据 flutter 中的所有可用空间? - How can i make pageView.Builder occupy all available space in flutter? 如何使其他流的新流返回所有发出的值的列表(bloc模式)? - How can i make a new stream of other streams returning a list of all the values emitted (bloc pattern)? 如何在 Cubit 中使用 CubitState 变量? 颤振/块 - How can I use variable of CubitState inside Cubit? Flutter/Bloc 如何取消 flutter / dart 集团中的“等待” - How can I cancel a 'await for' in a flutter / dart bloc flutter - bloc - 如何在我的 Ui 中使用 FutureBuilder 来正确实现 Bloc 架构 - flutter - bloc - how can I use FutureBuilder in my Ui to properly implement Bloc Architecture flutter_bloc-如果没有BlocBuilder,如何获得价值? - flutter_bloc - how can i get value without BlocBuilder? 如何将应用程序的状态更改为 flutter 中的块 - How can I change the status of an application to a bloc in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM