0
  titleColor: Colors.black,
    resizeToAvoidBottomInset: false,
    backgroundColor: Colors.white,
    body: ProviderListener(
        provider: addressNotifierProvider.state,
        onChange: (context, state) {
          if (state is AddressInitial) {
            print("hello");
            context
                .read(addressNotifierProvider)
                .fetchUserAddress(widget.addressIds);
          }
        },
        child: Consumer(
          builder: (context, watch, child) {
            final state = watch(addressNotifierProvider.state);
            print(state);
            if(state is AddressInitial){
              context
                  .read(addressNotifierProvider)
                  .fetchUserAddress(widget.addressIds);
            }
            if (state is AddressFetchedDetails) {
              return SingleChildScrollView(
                  child: Container(
                      color: Colors.white,
                      padding: EdgeInsets.only(

This giving me error as soon as page opens Unhandled Exception: setState() or markNeedsBuild() called during build. How to resolve this.

1 Answers1

0

The fetchUserAddress operation changes the State of the widget (because of the watch I guess). A state change triggers a rebuild. But you are not allowed to trigger a rebuild during a build operation, as the error explains.

To resolve this you should rethink your state management architecture.

However, as a workaround you can wrap all state changing calls in a post render frame callback.

WidgetsBinding.instance
        .addPostFrameCallback((_) => context
                  .read(addressNotifierProvider)
                  .fetchUserAddress(widget.addressIds) );
Stuck
  • 11,225
  • 11
  • 59
  • 104
  • i actually wanted to know if i can make my listener method to call the function but actually it is not working is there any workaround for that? – Sourav Munjal Jun 07 '21 at 23:51