title: Flutter - setState not updating a List inside a ListView.separated
body: <p>I am saving a list of favorites in my game app, I can add or remove games from the list, but on the removeFavorite method, when I use setState in "<strong>games.remove(index)</strong>" the listview.separeted doesn't update. If close and open favorite screen, the list is updated but while I am at the favorite screen it doesn't update.</p>
  
  <pre><code>class _FavoriteScreenState extends State&lt;FavoriteScreen&gt; {
  
    List&lt;dynamic&gt; favoriteList = [];
    List&lt;Game&gt; games = [];
  
    @override
    void initState() {
  
      loadFavorites();
  
      super.initState();
    }
  
    @override
    Widget build(BuildContext context) {
  
      Widget _buildListView(Game game, int index){
  
        return InkWell(
          child: Container(
            height: 80,
            child: Row(
              children: &lt;Widget&gt;[
                Expanded(
                  flex: 1,
                  child: Container(
                  margin: EdgeInsets.only(left: 5),
                    child: Image.network(game.cover),
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: Container(
                    child: Row(
                      children: &lt;Widget&gt;[
                        SizedBox(width: 15,),
                        Text(
                            game.title,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            color: Colors.white,
                            fontWeight: FontWeight.w300,
                            fontSize: 14
                          ),
                        ),
                      ],
                    ),
                  ),
                )
              ],
            ),
          ),
          onLongPress: (){
            _showDialog(index);
          },
        );
  
      }
  
      return Scaffold(
        backgroundColor: Theme.of(context).backgroundColor,
        appBar: AppBar(
          title: Text("Favorites"),
        ),
  
        body: Container(
  
          margin: EdgeInsets.fromLTRB(16, 16, 16, screenHeight &gt; 720 ? 90 : 62),
          child: ListView.separated(
            separatorBuilder: (BuildContext context, int index) =&gt; Divider(color: Colors.black,),
            itemCount: games.length,
              itemBuilder: (context, index){
  
                return _buildListView(games[index], index);
  
              },
          )
        ),
  
      );
    }
  
    Future&lt;File&gt; getFile() async{
  
      final directory = await getApplicationDocumentsDirectory();
      return File("${directory.path}/favorites.json");
  
    }
  
    Future&lt;String&gt; readFavorite() async{
  
      try{
  
        // Le e retorna o arquivo como String
        final file = await getFile();
        return file.readAsString();
  
      } catch (e){
        return null;
      }
  
  
    }
  
    void loadFavorites() {
  
      readFavorite().then((data){
  
        // Transforma o arquivo JSON numa List
        favoriteList = json.decode(data);
  
  
        if(favoriteList.length &gt; 0 &amp;&amp; favoriteList != null){
  
          favoriteList.forEach((map){
  
            Game game = Game(map["cover"], map["title"], map["description"], map["url"]);
  
            setState(() {
              games.add(game);
            });
  
  
          });
  
        } else {
  
        }
  
        print(games.length);
  
      });
  
  
    }
  
    Future&lt;File&gt; saveFile() async{
  
      String data = json.encode(favoriteList);
  
      final file = await getFile();
  
      return file.writeAsString(data);
  
    }
  
    void _showDialog(int index){
  
      showDialog(
          context: context,
          builder: (BuildContext context){
            return AlertDialog(
              content: Text("?",
                style: TextStyle(fontSize: 18) ,),
              actions: &lt;Widget&gt;[
                FlatButton(
                    child: Text("YES"),
                    onPressed: (){
                      Navigator.of(context).pop();
                      removeFavorite(index);
  
                    }
                ),
                FlatButton(
                    child: Text("NO"),
                    onPressed: (){
                      Navigator.of(context).pop();
                    }
                ),
              ],
            );
          }
      );
  
    }
  
    void removeFavorite(int index){
  
      favoriteList.forEach((m) {
        Map&lt;String, dynamic&gt; map = m;
  
        if (map.containsValue(games[index].title)) {
          favoriteList.remove(m);
  
          saveFile();
          setState(() {
            games.remove(index);
          });
  
        }
      });
  
    }
  
  }
  </code></pre>