等待未来的构建者完成后再继续

时间:2019-02-04 18:48:10

标签: asynchronous flutter future

我有一个将来运行的构建器块,在继续调用该函数本身之前,我需要它来加载视频。

您看不到下一张照片: currentMainImage:x showImage()等待waitTime 快照... 快照...

Future<void> showImage() async {
int waitTime;
String type;

(currentImage == playList.length) ? currentImage = 0 : currentImage =
    currentImage;
waitTime = int.parse(playList[currentImage]["transTime"]);
print("showImage() - Waiting $waitTime s.");

Future.delayed(Duration(seconds: waitTime), () {
  print("IM INSIDE FUTURE DELAYED");
  setState(() {

    print("SETTING STATE");

    if(playerController.value.isPlaying) {
      playerController.setVolume(0.0);
      playerController.removeListener(listener);
      print("PLAYER IS PLAYING _ DISABLE");
    }

    currentImage++;
    (currentImage == playList.length) ? currentImage = 0 : currentImage = currentImage;

    type = playList[currentImage]["type"];

    print("TYPE: $type");

    if(type == "image") {
      print("SETTING IMAGE:");
      currentMainImage = getRealFilename(playList[currentImage]["file"]);
      print("CurrentMainImage: $currentMainImage");
      playlistWidget = new FutureBuilder(
          future: _getLocalFile(currentMainImage),
          builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
            print("SNAPSHOT : ${snapshot.data}");
            if(snapshot.data != null) {
              return Padding(
                padding: EdgeInsets.only(bottom: 22.0, left: 7.0),
                child: Container(
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      fit: BoxFit.fill,
                      image: FileImage(snapshot.data),
                    ),
                  ),
                  height: 326.0,
                  width: 590.0,
                  alignment: Alignment.center,
                )
              );
            } else {
              return CircularProgressIndicator();
            }
          }
      );
    } else if(type == "mp4") {
      print("SETTING VIDEO:");
      currentMainVideo = getRealFilename(playList[currentImage]["file"]);
      playlistWidget = new FutureBuilder(
          future: _getLocalFile(currentMainVideo),
          builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
            print("SNAPSHOT : ${snapshot.data}");
            if (snapshot.data != null) {
              playerController = VideoPlayerController.file(snapshot.data)
                ..setVolume(0.0)
                ..initialize()
                ..play();

              playerController.addListener(listener);

              return Padding(
                padding: EdgeInsets.only(bottom: 22.0,left: 7.0),
                child: Container(
                  height: 326.0,
                  width: 590.0,
                  alignment: Alignment.center,
                  child: VideoPlayer(
                      playerController
                  ),
                ),
              );
            } else {
              return CircularProgressIndicator();
            }
          }
      );
    } else {
      print("BIG ERROR");
    }
    print("CALLING SHOWIMAGE()");
    showImage();
  });
});
}

实际结果是,当我输入setState且类型为video时,它甚至在显示视频之前再次调用showImage()。导致两次调用该函数并使应用卡住。

0 个答案:

没有答案