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

时间:2019-05-24 07:19:14

标签: dart flutter

列表视图中的数据未更新,但响应正在日志中打印。我想在列表视图中显示数据,但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);
  }

1 个答案:

答案 0 :(得分:0)

此代码应该有效:

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

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $specials = '!@#$%^&*()';
    $charactersLength = strlen($characters);
    $randomString = '';
    // Removed one from length to maintain desired length
    // for special character addition
    for ($i = 0; $i < $length - 1; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    // Add the special character:
    $randomString .= $specials[rand(0, strlen($specials) - 1)];
    // Shuffle the returned string so the special is not always at the end
    return str_shuffle($randomString);
}

您不需要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