title: AnimatedList in a StreamBuilder (and widget removes itself)
body: <p>I'm trying to make a Favorites page which takes a list of marked favorites as ListTile() and let the user disable the favorite. I use a StreamController to manage the favorites list as it is shared through the whole app.</p>
  <p>So my favorite page is like this :</p>
  <pre class="lang-dart prettyprint-override"><code>class FavoritesListPageBody extends StatelessWidget
  {
      FavoritesListPageBody({Key? key}) : super(key: key);
      final _animatedListKey = new GlobalKey&lt;AnimatedListState&gt;();
  
      @override
      Widget build(BuildContext context)
      {
          final ScoresBloc bloc = BlocProvider.of&lt;ScoresBloc&gt;(context);
          
          return StreamBuilder&lt;List&lt;Score&gt;&gt;(
              stream: bloc.favorites,
              initialData: bloc.favoritesManager.favorites,
              builder: (BuildContext context, AsyncSnapshot&lt;List&lt;Score&gt;&gt; snapshot)
              {       
                  if(bloc.favoritesManager.favorites.isEmpty)
                  {
                      return Container(
                          constraints: const BoxConstraints.expand(),
                          color: Theme.of(context).colorScheme.favListEmptyBackground,
                          child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                  Icon(Icons.search_rounded, size: 120, color: Theme.of(context).colorScheme.noFavoriteFound),
                                  const SizedBox(height: 50,),
                                  Text(&quot;Aucun favori&quot;, style: TextStyle(color: Theme.of(context).colorScheme.noFavoriteFound)),
                              ],
                          )
                      );
                  }
  
                  
                  for(Score fav in bloc.favoritesManager.favorites)
                  {
                      this._animatedListKey.currentState?.insertItem(bloc.favoritesManager.favorites.indexOf(fav));
                  }
                  
  
                  return AnimatedList(
                      key: this._animatedListKey,
                      initialItemCount: bloc.favoritesManager.favorites.length,
                      itemBuilder: (BuildContext context, int index, Animation&lt;double&gt; animation)
                      {
                          var curved = new CurvedAnimation(parent: animation, curve: const Interval(.5, 1));
                          
                          Score score = bloc.favoritesManager.favorites[index];
  
                          return SizeTransition(
                              sizeFactor: CurvedAnimation(parent: animation, curve: const Interval(0, .5)),
                              child: SlideTransition(
                                  position: Tween(
                                      begin: const Offset(1, 0),
                                      end: const Offset(0, 0)
                                  ).animate(curved),
                                  child: Column(
                                      children: [
                                          ScoreTile(
                                              score: score,
                                              onPressed: ()
                                              {
                                                  bloc.toggleFavorite(score);
                                                  _animatedListKey.currentState!.removeItem(
                                                      index,
                                                      (context, animation) =&gt; Column(children: [
                                                          ScoreTile(score: score),
                                                          Divider(height: 1, color: Theme.of(context).colorScheme.separator)
                                                      ]),
                                                      duration: const Duration(seconds: 1)
                                                  );
                                              },
                                          ),
                                          Divider(height: 1, color: Theme.of(context).colorScheme.separator)
                                      ]
                                  )
                              )
                          );
                      },
                  );
              }
          );
      }
  }
  </code></pre>
  <p>Adding favorites works well wherever I do it. Removing favs works fine if I do it in all pages except for this one. The code above works but not the animation as I did not use &quot;insertItem&quot; before... To this is &quot;static&quot; like a basic ListView.</p>
  <p>When I use insertItem() while building the AnimatedList, and then try to remove a favorite from this page I obtain this error :
  <code>RangeError (RangeError (index): Invalid value: Not in inclusive range 0..2: 3)</code></p>
  <p>I think I can not use StreamBuilder() and insertItem()/removeItem() together to achieve this, as it will cause &quot;indexes&quot; issues...</p>
  <p>How could I do this ?</p>