title: Flutter Provider setState() or markNeedsBuild() called during build
body: <p>I want to load a list of events and display a loading indicator while fetching data.</p>
  
  <p>I'm trying <strong>Provider</strong> pattern (actually refactoring an existing application).</p>
  
  <p>So the event list display is conditional according to a status managed in the provider.</p>
  
  <p>Problem is when I make a call to <code>notifyListeners()</code> too quickly, I get this exception :</p>
  
  <blockquote>
    <p>════════ Exception caught by foundation library ════════</p>
    
    <p>The following assertion was thrown while dispatching notifications for EventProvider:</p>
    
    <p>setState() or markNeedsBuild() called during build.</p>
    
    <p>...</p>
    
    <p>The EventProvider sending notification was: Instance of 'EventProvider'</p>
    
    <p>════════════════════════════════════════</p>
  </blockquote>
  
  <p>Waiting for some milliseconds before calling <code>notifyListeners()</code> solve the problem (see commented line in the provider class below).</p>
  
  <p>This is a simple example based on my code (hope not over simplified) :</p>
  
  <p><strong>main function</strong> :</p>
  
  <pre class="lang-dart prettyprint-override"><code>Future&lt;void&gt; main() async {
      runApp(
          MultiProvider(
              providers: [
                  ChangeNotifierProvider(create: (_) =&gt; LoginProvider()),
                  ChangeNotifierProvider(create: (_) =&gt; EventProvider()),
              ],
              child: MyApp(),
          ),
      );
  }
  </code></pre>
  
  <p><strong>root widget</strong> :</p>
  
  <pre class="lang-dart prettyprint-override"><code>class MyApp extends StatelessWidget {
  
      @override
      Widget build(BuildContext context) {
          final LoginProvider _loginProvider = Provider.of&lt;LoginProvider&gt;(context, listen: true);
          final EventProvider _eventProvider = Provider.of&lt;EventProvider&gt;(context, listen: false);
  
          // load user events when user is logged
          if (_loginProvider.loggedUser != null) {
              _eventProvider.fetchEvents(_loginProvider.loggedUser.email);
          }
  
          return MaterialApp(
              home: switch (_loginProvider.status) { 
                  case AuthStatus.Unauthenticated:
                      return MyLoginPage();
                  case AuthStatus.Authenticated:
                      return MyHomePage();
              },
          );
  
      }
  }
  </code></pre>
  
  <p><strong>home page</strong> :</p>
  
  <pre class="lang-dart prettyprint-override"><code>class MyHomePage extends StatelessWidget {
  
      @override
      Widget build(BuildContext context) {
          final EventProvider _eventProvider = Provider.of&lt;EventProvider&gt;(context, listen: true);
  
          return Scaffold(
              body: _eventProvider.status == EventLoadingStatus.Loading ? CircularProgressIndicator() : ListView.builder(...)
          )
      }
  }
  </code></pre>
  
  <p><strong>event provider</strong> :</p>
  
  <pre class="lang-dart prettyprint-override"><code>enum EventLoadingStatus { NotLoaded, Loading, Loaded }
  
  class EventProvider extends ChangeNotifier {
  
      final List&lt;Event&gt; _events = [];
      EventLoadingStatus _eventLoadingStatus = EventLoadingStatus.NotLoaded;
  
      EventLoadingStatus get status =&gt; _eventLoadingStatus;
  
      Future&lt;void&gt; fetchEvents(String email) async {
          //await Future.delayed(const Duration(milliseconds: 100), (){});
          _eventLoadingStatus = EventLoadingStatus.Loading;
          notifyListeners();
          List&lt;Event&gt; events = await EventService().getEventsByUser(email);
          _events.clear();
          _events.addAll(events);
          _eventLoadingStatus = EventLoadingStatus.Loaded;
          notifyListeners();
      }
  }
  </code></pre>
  
  <p>Can someone explain what happens?</p>