title: Using a CachedVideoPlayer in a listview
body: <p>I am attempting to show videos in a <code>listview</code> that is preventing me from declaring the <code>videocontroller</code> in the <code>initState</code>. This causes me to accidentally be redrawing the video multiple times during the application. I am receiving this error:</p>
  <pre><code>FATAL EXCEPTION: ExoPlayerImplInternal:Handler
  </code></pre>
  <p>then</p>
  <pre><code>java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
  </code></pre>
  <p>with my current implementation. It appears to work fora while but the memory slowly builds up until it is full. How can I implement this differently?</p>
  <p>here is the code I am calling in the stream:</p>
  <pre><code> Widget getVideoItem(DocumentSnapshot doc) {
      if (watchList.contains(doc['user'])) watched = true;
      DateTime dateTime = DateTime.parse(doc['time']);
  
      _videoPlayerController = CachedVideoPlayerController.network(doc[&quot;downUrl&quot;])
        ..initialize();
      _videoPlayerController.setLooping(true);
      _videoPlayerController.play();
  
      volumeOn = sharedPreferences.getBool(&quot;vidVol&quot;);
      if (volumeOn == null) {
        sharedPreferences.setBool(&quot;vidVol&quot;, false);
        volumeOn = false;
      }
  
      if (volumeOn) {
        _videoPlayerController.setVolume(1.0);
      } else {
        _videoPlayerController.setVolume(0.0);
      }
  
      return new FutureBuilder(
        future: getUserData(doc[&quot;user&quot;]),
        builder: (BuildContext context, snapshot) {
          return SizedBox(
            height: MediaQuery.of(context).size.width + 140,
            width: MediaQuery.of(context).size.width,
            child: Column(children: &lt;Widget&gt;[
              new ListTile(
                title: new Text(userInfo),
                subtitle: new Text(doc[&quot;title&quot;]),
                leading: FutureBuilder(
                    future: getProfUrl(doc),
                    builder: (BuildContext context, snapshot) {
                      Widget child;
                      if (!snapshot.hasData) {
                        child = _showCircularProgress();
                      } else {
                        child = child = new Container(
                          width: 44.0,
                          height: 44.0,
                          child: CachedNetworkImage(
                            imageUrl: doc[&quot;profUrl&quot;],
                            imageBuilder: (context, imageProvider) =&gt; Container(
                              decoration: BoxDecoration(
                                shape: BoxShape.circle,
                                image: DecorationImage(
                                  image: imageProvider,
                                  fit: BoxFit.cover,
                                ),
                              ),
                            ),
                          ),
                        );
                      }
                      return child;
                    }),
              ),
              new Padding(
                padding: EdgeInsets.fromLTRB(4, 4, 4, 4),
                child: FutureBuilder(
                    future: getDownUrl(doc),
                    builder: (BuildContext context, snapshot) {
                      List&lt;Widget&gt; children;
                      if (!snapshot.hasData) {
                        children = [_showCircularProgress()];
                      } else {
                        children = [
                          Center(
                            child: new AspectRatio(
                              aspectRatio: 1 / 1,
                              child: Stack(
                                children: [
                                  VisibilityDetector(
                                      key: Key(&quot;unique key&quot;),
                                      onVisibilityChanged: (VisibilityInfo info) {
                                        if (info.visibleFraction &gt; .20) {
                                          _videoPlayerController.pause();
                                        } else {
                                          _videoPlayerController.play();
                                        }
                                      },
                                      child: CachedVideoPlayer(
                                        _videoPlayerController,
                                      )),
                                  IconButton(
                                    icon: volumeOn
                                        ? Icon(Icons.volume_up)
                                        : Icon(Icons.volume_off),
                                    onPressed: () {
                                      setState(() {
                                        _videoPlayerController.pause();
                                        sharedPreferences.setBool(
                                            &quot;vidVol&quot;, !volumeOn);
                                      });
                                    },
                                  ),
                                ],
                              ),
                            ),
                          )
                        ];
                      }
  
                      return Center(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: children,
                        ),
                      );
                    }),
              ),
              new Row(
                children: [
                  new IconButton(
                      icon: !watched
                          ? new Icon(
                              Icons.remove_red_eye,
                              color: Colors.black26,
                            )
                          : new Icon(
                              Icons.remove_red_eye,
                              color: Colors.blueGrey[400],
                            ),
                      onPressed: () {
                        initToggleWatched(watchList, doc[&quot;user&quot;], name, position,
                            secPosition, state, year, user);
                      }),
                  Padding(
                    padding: EdgeInsets.fromLTRB(5, 0, 0, 0),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        dateTime.day.toString() +
                            &quot;/&quot; +
                            dateTime.month.toString() +
                            &quot;/&quot; +
                            dateTime.year.toString(),
                        style: TextStyle(color: Colors.black26, fontSize: 12),
                      ),
                    ),
                  ),
                ],
              )
            ]),
          );
        },
      );
    }
  </code></pre>