title: Flutter Error: setState() or markNeedsBuild() called during build
body: <p>I am fetching data from the server and I store the data in a list, then I pass some part of that data to a provider class which some other widgets are listening to that provider class but I don't know why when I run the app it gives me the above error, although the app runs but the UI does not updates when the data changes. It seems that it is not listening to that. But I am sure that I added notify listeners method to that.</p>
  <p>Here is my code:
  provider class:</p>
  <pre><code>        import 'package:flutter/material.dart';
           class EmpAndState with ChangeNotifier {
           final String empId;
           String stateId;
           EmpAndState({@required this.empId, this.stateId = &quot;1&quot;});
            }
  
           class EmpsAstates with ChangeNotifier {
           List&lt;EmpAndState&gt; _items = [];
           List&lt;EmpAndState&gt; get items {
           return [..._items];
           }
  
           void addAll(List&lt;String&gt; empIdes) {
          final eIds = empIdes.map((e) =&gt; EmpAndState(empId: e)).toList();
         _items = eIds;
         notifyListeners();
         }
  
  
  // void addItem(String eId, String stId) {
  //   _items.add(EmpAndState(empId: eId, stateId: stId));
  //   notifyListeners();
  // }
  
  void updateItem(String eId, String stId) {
   _items[_items.indexWhere((es) =&gt; es.empId == eId)] =
      EmpAndState(empId: eId, stateId: stId);
    notifyListeners();
     }
  
   EmpAndState findById(String eId) {
   return _items.firstWhere((es) =&gt; es.empId == eId);
   }
   }
  </code></pre>
  <p>Listener class:</p>
  <pre><code>    import 'package:Attendece/provider/empAstate.dart';
      import 'package:flutter/material.dart';
      import 'package:provider/provider.dart';
      import '../provider/employeeAtypes.dart';
  
      class ListTileRow extends StatelessWidget {
      static const IconData cancel_outlined =
      IconData(0xe0c9, fontFamily: 'MaterialIcons');
  
    Widget build(BuildContext context) {
    Employee emp = Provider.of&lt;Employee&gt;(context);
    final empAsta = Provider.of&lt;EmpsAstates&gt;(context);
    final eas = empAsta.findById(emp.employeeId);
   return Row(
    mainAxisSize: MainAxisSize.min,
    mainAxisAlignment: MainAxisAlignment.end,
    children: [
      IconButton(
          icon: eas.stateId == &quot;1&quot;
              ? Icon(Icons.check_circle, color: Color(0xff507ce0))
              : Icon(Icons.check_circle_outline),
          onPressed: () {
            if (eas.stateId != &quot;1&quot;) {
              eas.stateId = &quot;1&quot;;
            } else {
              eas.stateId = &quot;2&quot;;
            }
            emp.notifyListeners();
            empAsta.updateItem(emp.employeeId, eas.stateId);
          }),
      IconButton(
          icon: eas.stateId == &quot;2&quot;
              ? Icon(Icons.clear, color: Colors.red)
              : Icon(Icons.clear),
          onPressed: () {
            if (eas.stateId != &quot;2&quot;) {
              eas.stateId = &quot;2&quot;;
              emp.notifyListeners();
              empAsta.updateItem(emp.employeeId, eas.stateId);
            } else {
              eas.stateId = &quot;1&quot;;
              emp.notifyListeners();
              empAsta.updateItem(emp.employeeId, eas.stateId);
            }
          }),
      IconButton(
          icon: eas.stateId == &quot;3&quot;
              ? Icon(Icons.airline_seat_flat, color: Color(0xff507ce0))
              : Icon(Icons.airline_seat_flat),
          onPressed: () {
            if (eas.stateId != &quot;3&quot;) {
              eas.stateId = &quot;3&quot;;
              emp.notifyListeners();
              empAsta.updateItem(emp.employeeId, eas.stateId);
            } else {
              eas.stateId = &quot;1&quot;;
              emp.notifyListeners();
              empAsta.updateItem(emp.employeeId, eas.stateId);
            }
          }),
      IconButton(
          icon: eas.stateId == &quot;5&quot;
              ? Icon(Icons.home, color: Color(0xff507ce0))
              : Icon(Icons.home),
          onPressed: () {
            if (eas.stateId != &quot;5&quot;) {
              eas.stateId = &quot;5&quot;;
              emp.notifyListeners();
              empAsta.updateItem(emp.employeeId, eas.stateId);
            } else {
              eas.stateId = &quot;1&quot;;
              emp.notifyListeners();
              empAsta.updateItem(emp.employeeId, eas.stateId);
            }
          }),
    ],
  );
  }
  }
  </code></pre>