我想在ListView.builder中创建一个视频播放器。我的视频存储在Firestore中。在ListView.builder中加载视频时没有问题,但无论何时向上和向下滚动页面,我都会在红色框中显示错误:
处理后使用VideoPlayerController。
I / flutter:一旦你在VideoPlayerController上调用dispose(),就不能再使用了它。
Widget _modeApp(){
return new ListView.builder(
itemCount: dataApp.length,
itemBuilder: (context,i){
String _path = dataApp[i].url;
_controllers.add(new TextEditingController());
_focusNode.add(new FocusNode());
return new Card(
child: new Column(
children: <Widget>[
new Row(
children: <Widget>[
new Container(
width: 50.0,
height: 50.0,
margin: const EdgeInsets.all(15.0),
decoration: new BoxDecoration(
border: new Border.all(color: Colors.black38),
shape: BoxShape.circle,
image: new DecorationImage(image: new NetworkImage(dataApp[i].aurl))
),
),
new Text(dataApp[i].name,style: new TextStyle(fontWeight: FontWeight.bold)),
],
),
new Chewie(
VideoPlayerController.network(_path),
aspectRatio: 3 / 2,
autoPlay: false,
looping: false,
),
new Container(
margin: const EdgeInsets.only(left: 12.0,top: 12.0),
child: new Row(
children: <Widget>[
new Icon(Icons.favorite_border,size: 27.0),
new Padding(padding: EdgeInsets.only(left: 15.0)),
new Icon(Icons.chat_bubble_outline,size: 27.0),
new Padding(padding: EdgeInsets.only(left: 15.0)),
new Icon(Icons.turned_in_not,size: 27.0),
],
),
),
new ListTile(
title: new Text(dataApp[i].text,style: new TextStyle(fontWeight: FontWeight.bold),),
subtitle: new Text(dataApp[i].time),
),
new ListTile(
leading: new Container(
width: 50.0,
height: 50.0,
decoration: new BoxDecoration(
border: new Border.all(color: Colors.black38),
shape: BoxShape.circle,
image: widget.accountEmail == null
? new DecorationImage(image: new NetworkImage("${widget.value.photoUrl}"))
: new DecorationImage(image: new NetworkImage("${widget.accountPhoto}")),
),
),
title: new EnsureVisibleWhenFocused(
focusNode: _focusNode[i],
child: new TextField(
focusNode: _focusNode[i],
controller: _controllers[i],
style: new TextStyle(
fontSize: 15.0,
color: Colors.black
),
decoration: new InputDecoration(
border: InputBorder.none,
hintText: "Add a comment...",
hintStyle: new TextStyle(color: Colors.grey),
),
),
),
trailing: new IconButton(
icon: new Icon(Icons.send, color: _controllers[i].text != "" ? Colors.blue : null), onPressed: _controllers[i].text != "" ? () => _showMessage(i) : null),
),
new Padding(
padding: EdgeInsets.only(top: 13.0)),
],
),
);}
);
}
您不应该在构建方法中创建视频播放器控制器。您希望在构建方法之外创建控制器,以便在滚动时不再重新创建。