The getter 'bloc' called on null

Viewed 630

I tried to create listview using bloc.

this is my bloc class

MainPageList model looks like this

class MainPageList { List items; List icons;
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();
  }
}

In widget class I tried to call bloc.

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

assign stream,

              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),

assign to icons

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

assign to items

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

You are not providing enough code to know the context of your build method but it seems that you are using a BlocProvider. You should know that in order to have the bloc in the context of your widget you have to call it this way:

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();
  }
}

As you can see, home of my app is not directly MyHome but a BlocProvider widget.

Related