Search code examples
flutterflutter-provider

Flutter: setState() or markNeedsBuild() called during build with Provider


I'm calling one of the ChangeNotifier functions from an initState() method and the notifyListener() call inside the function is throwing setState() or markNeedsBuild() called during build. exception

void initState(){
 Provider.of<MessengerRepository>(context, listen: false).setUnreadCount(0);
 super.initState();
}
class MessengerRepository with ChangeNotifier {
  int unreadCount;
  void setUnreadCount(int value){
     unreadCount = value;
     notifyListeners();
}

I need to call the notifyListener() inside setUnreadCount() as I'm calling the setUnreadCount() function in multiple places during the execution. So can't remove the notifyListener() only inside initState().

Using provider version 4.0.4


Solution

  • The cause for this issue as mentioned in the log message is that the function inside initState() is requesting a rebuild through notifyListener() (the same will happen if we use setState() as well) even before the first build is completed.

    The solution is to add a addPostFrameCallback and execute the rebuilding function inside it so that it will execute only after the first building of the widget

    void initState() {
        super.initState();
        WidgetsBinding
         .instance
         .addPostFrameCallback((_){ 
          Provider.of<MessengerRepository>(context, listen: false).setUnreadCount(0);
         }
        );
      }