gpt4 book ai didi

dart - 我正在将dio库用于http响应,但未更新listview抖动

转载 作者:行者123 更新时间:2023-12-03 04:01:16 24 4
gpt4 key购买 nike

列表 View 中的数据没有更新,但是响应正在日志中打印。我想在列表 View 中显示数据,但API正常工作,视频未更新,并且卡中没有收到任何数据。如果有解决此问题的想法。

在代码下面,我点击了API以获取响应,但未显示任何内容(视频)。
我正在使用流生成器来实现数据显示,请检查以下代码并告诉我问题出在哪里

class RespondedVideos extends StatefulWidget{

var slug;
RespondedVideos({Key key,@required this.slug}) : super(key: key);

@override
respondedVideosState createState() => respondedVideosState();

}

class respondedVideosState extends State<RespondedVideos> {

VideoPlayerController _controller;
List data = new List();
var respondercookie;
String video_url = "";

@override
void initState() {
super.initState();
readCookie();
}

@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Flexible(
flex: 1,
child:Container(
height: 100,
child: Card(
margin: EdgeInsets.fromLTRB(3.0, 60.0, 3.0, 0.0),
color: Colors.blueAccent,
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Text("Feedback: ")
],
),
Row(
children: <Widget>[
Text("Status: ")
//+data[position].name,style:TextStyle(color: Colors.blueAccent)
],
)
]
),
),

)
),
SizedBox(height: 10),
Flexible(
flex: 2,
child:Container(
height: 1000,
child: StreamBuilder(
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.tealAccent),
),
);
} else {
return ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
padding: EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 0.0),
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int position) {
if(data[position]["source_url"] != null)
video_url = data[position]["source_url"];
_controller = VideoPlayerController.network(video_url
)
..initialize().then((_) {
setState(() {});
});
_controller.setVolume(30.0);
return new Container(
child: Center(
child: Card(
child: Container(
width: 230.0,
child: Column(
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Stack(
children: <Widget>[
Container(
child: _controller.value.initialized
? AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer(_controller),

)
: Container(),
),

Column(
children: <Widget>[
Padding(padding: new EdgeInsets.symmetric(
vertical: 80.0, horizontal: 160.0)),
RaisedButton(
color: Colors.transparent,
onPressed: () {
setState(() {
_controller.value.isPlaying
? _controller.pause()
: _controller.play();
});
},
child: Icon(
_controller.value.isPlaying
? Icons.pause
: Icons.play_arrow,
color: Colors.blueAccent,
),
),
],
),
],
),

],
),
),
),
)
);
}
);
}
}
)
)
),

],
);

}
Future<String>getUserVideoDetails(String slug) async{
String url= "http://3.208.3.10:3000/api/v1/feedbacks/"+slug+"";
var response = await http.get(
Uri.encodeFull(url),
headers: {"Content-Type": "application/json","cookie":respondercookie},
);
setState(() {
var convertDataToJson = json.decode(response.body);
data =convertDataToJson['responses'];
print(data);
});
return "suceess";
}



void readCookie() async{
final prefs = await SharedPreferences.getInstance();
respondercookie = prefs.getString('respondedcookie')??'';
getUserVideoDetails(widget.slug);
}

最佳答案

此代码应工作:

提取数据不会在setState中打印:

Future<String>getUserVideoDetails(String slug) async{
String url= "http://3.208.3.10:3000/api/v1/feedbacks/"+slug+"";
var response = await http.get(
Uri.encodeFull(url),
headers: {"Content-Type": "application/json", "cookie": respondercookie},
);
var convertDataToJson = json.decode(response.body);
setState(() {
data = convertDataToJson['responses'];
});
print(data);
return "suceess";
}

您不需要 StreamBuilder,只需使用 ListView.builder即可:
Flexible(
flex: 2,
child: Container(
height: 1000,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
padding: EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 0.0),
itemCount: data.length,
itemBuilder: (BuildContext context, int position) {
if (data.isEmpty) {
return Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(
Colors.tealAccent),
),
);
}
if (data[position]["source_url"] != null)
video_url = data[position]["source_url"];
_controller = VideoPlayerController.network(video_url
)
..initialize().then((_) {
setState(() {});
});
_controller.setVolume(30.0);
return new Container(
child: Center(
child: Card(
child: Container(
width: 230.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Stack(
children: <Widget>[
Container(
child: Container(),
child: _controller.value.initialized
? AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer(_controller),

)
: Container(),
),
Column(
children: <Widget>[
Padding(
padding: new EdgeInsets.symmetric(
vertical: 80.0,
horizontal: 160.0)),
RaisedButton(
color: Colors.transparent,
onPressed: () {
setState(() {
_controller.value.isPlaying
? _controller.pause()
: _controller.play();
});
},
child: Icon(
_controller.value.isPlaying
? Icons.pause
Icons.play_arrow,
color: Colors.blueAccent,
),
),
],
),
],
),
],
),
),
),
));
}))),

关于dart - 我正在将dio库用于http响应,但未更新listview抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56287635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com