0

I have been struggling with the problem of pushing updated data from one widget to another. This problem occurs when I have two Stateful widgets and the data is updated in the parent Stateful widget but is not updated in the child Stateful widget. The error occurs with the usage of the freezed package but also occurs without it as well.

I have not been able to find anything that fixes this as of yet.

Below is an example:

First Stateful Widget:

class FirstWidget extends StatefulWidget {
  @override
  _FirstWidgetState createState() => _FirstWidgetState();
}

class _FirstWidgetState extends State<FirstWidget> {

  ItemBloc _itemBloc = getit<ItemBloc>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
          backgroundColor: Colors.black,
          appBar: AppBar(
            elevation: Mquery.width(context, 2.5),
            backgroundColor: Colors.black
            title: Text(
              'First stateful widget',
              style: TextStyle(fontSize: 17),
            ),
            centerTitle: true,
          ),
      body: BlocBuilder<ItemsBloc,ItemsState>(
             cubit: _itemsBloc,
             builder: (BuildContext context,ItemState state) {
                     return state.when(
                               initial: () => Container(),
                               loading: () => Center(child: CustomLoader()),
                               success: (_items) {
                                     return AnotherStatefulWidget(
                                             items: _items,
                                             ...
                                     );
                                  },
                               );
                           },
                        ));
                      },
                  );
);
  }
}

Second Stateful Widget:

class AnotherStatefulWidget extends StatefulWidget {
  final List<String> items;

  AnotherStatefulWidget(this.items);

  @override
  _AnotherStatefulWidgetState createState() => _AnotherStatefulWidgetState();
}

class _AnotherStatefulWidgetState extends State<AnotherStatefulWidget> {
  final ScrollController scrollController = ScrollController();
  ItemsBloc _itemsBloc = getit<ItemsBloc>();

  bool _handleNotification(ScrollNotification notification, List<String> items) {
    if (notification is ScrollEndNotification &&
        scrollController.position.extentAfter == 0.00) {
      _itemsBloc.add(ItemsLoadEvent.loadMoreItems(
          categories: items, document: ...));
    }

    return false;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                width: double.infinity,
                height: 280,
                child: Padding(
                  padding: EdgeInsets.only(
                    right: 8,
                  ),
                  child: NotificationListener<ScrollNotification>(
                    onNotification: (_n) =>
                        _handleNotification(_n, widget.items),
                    child: DraggableScrollbar.arrows(
                      alwaysVisibleScrollThumb: true,
                      controller: scrollController,
                      child: ListView.builder(
                        controller: scrollController,
                        itemCount: widget.items.length,
                        itemBuilder: (context, index) {
                          return GestureDetector(
                            child: Padding(
                              padding: EdgeInsets.all(16),
                              child: Align(
                                  alignment: Alignment.center,
                                  child: Text(
                                    widget.items[index],
                                    style: TextStyle(color: Colors.white),
                                  )),
                            ),
                          );
                        },
                      ),
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

I would really appreciate any help!

Thank you for you time.

dotmp3
  • 494
  • 4
  • 13
  • Make a reference of child widget in the parent widget and call the child method from the parent widget. – Jaya Prakash Sep 22 '20 at 00:56
  • By reference what are you referring to the state or the widget? And which child method are you referring to? – dotmp3 Sep 22 '20 at 01:09
  • it's better if you use some state management tool. check this article from the official flutter site: [https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple] – Besufkad Menji Sep 22 '20 at 10:08
  • I'm not sure how you would use one of the state management tools in the docs to solve this problem, because I don't see anything there dealing with two stateful widgets and passing data from one to the other. Would you be kind enough to link an article where this is done or give an example please? – dotmp3 Sep 23 '20 at 03:59

0 Answers0