吸气剂“ bloc”调用null

时间:2019-07-03 08:08:36

标签: flutter dart bloc

我试图使用bloc创建listview。

这是我的集团课程

MainPageList模型如下

  

class MainPageList {列出项目;列出图标;
  MainPageList(this.items,this.icons); }

class MainPageBloc extends BlocBase {
  MainPageList _mainPageList;
  StreamController<MainPageList> _mainPageController =
      StreamController<MainPageList>.broadcast();
  StreamSink<MainPageList> get _inMainPage => _mainPageController.sink;
  Stream<MainPageList> get outMainPage => _mainPageController.stream;

  MainPageBloc(context) {
    init(context);
  }

  void init(BuildContext context) async {
    _mainPageList.items = [
      AppTranslations.of(context).text("submit_request"),
      AppTranslations.of(context).text("signout")
    ];
    _mainPageList.icons = [
      "lib/assets/images/submit_req.svg",
      "lib/assets/images/sign_out.svg"
    ];
    _inMainPage.add(_mainPageList);
  }

  @override
  void dispose() {
    _mainPageController.close();
  }
}

在小部件类中,我试图调用bloc。

@override
  Widget build(BuildContext context) {
    final MainPageBloc mainBloc = BlocProvider.of<MainPageBloc>(context);
    return WillPopScope(
      onWillPop: () async => false,
      child: Scaffold(
          body: StreamBuilder<MainPageList>(

分配流

              stream: mainBloc.outMainPage,
              builder: (context, snapshot) {
                Container(
                  child: Scaffold(
                    body: Container(
                      child: ListView.builder(
                        scrollDirection: Axis.vertical,
                        shrinkWrap: true,
                        itemCount: snapshot.data.items.length,
                        itemBuilder: (BuildContext context, int index) {
                          return Card(
                            child: Container(
                                child: ListTile(
                                  contentPadding: EdgeInsets.symmetric(
                                      horizontal: 5.0, vertical: 0.0),

分配给图标

                                  leading: Container(
                                    child: SvgPicture.asset(
                                      snapshot.data.icons[index],
                                      width: 40.0,
                                      color: const Color(0xFFE27023),
                                    ),
                                  ),

分配给项目

                                  title: Text(
                                    snapshot.data.items[index],
                                    style: TextStyle(
                                        color: Colors.black,
                                        fontWeight: FontWeight.bold),
                                  ),

1 个答案:

答案 0 :(得分:1)

您没有提供足够的代码来了解构建方法的上下文,但似乎您正在使用BlocProvider。您应该知道,为了在您的小部件上下文中包含该块,您必须使用以下方式调用它:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
        home: BlocProvider<MyBloc>(
              bloc: MyBloc(),
              child: MyHome(),
            ),
    );
  }
}

class MyHome extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    myBloc = BlocProvider.of<MyBloc>(context);
    return Container();
  }
}

如您所见,我的应用程序的主目录不是MyHome,而是BlocProvider小部件。