简体   繁体   中英

Cannot share a file as it need to wait a future

Share() must display a modal in order to let the user wait in front of a circular progress indicator while I am loading the video file URL.

My code is as below, but I am puzzled about how to architecture: I need to trigger the sharing only once the snapshot.hasData .

How can that be done?

Btw, I use share_plus

 Future<void> share(BuildContext context) async {
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return FutureBuilder(
              future: Video.videoUrl(videoUrl!),
              builder: (context, snapshot) {
                final file = XFile(snapshot.data!);
                Share.shareXFiles([file],
                    text: "Don't miss this out! Only on Shokaze");

                return SizedBox(
                    height: 200,
                    child: Center(
                        child: !snapshot.hasData
                            ? Column(children: [
                                Text("Preparing sharing…"),
                                const CircularProgressIndicator(),
                              ])
                            : Text("Sharing…")));
              });
        });
  }

You should refactor the FutureBuilder using if/else conditions:

 if (snapshot.hasData) {
            Share.share(snapshot.data!);
            return Text(snapshot.data.toString());
          } else if (snapshot.hasError) {
            return Text(snapshot.error.toString());
          } else {
            return Scaffold(body: Center(child: CircularProgressIndicator()));
          }
Future<void> share(BuildContext context) async {
  showModalBottomSheet(
    context: context,
    builder: (context) {
      return FutureBuilder(
        future: myFuture(),
        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
          if (snapshot.hasData) {
            Share.share(snapshot.data!);
            return Text(snapshot.data.toString());
          } else if (snapshot.hasError) {
            return Text(snapshot.error.toString());
          } else {
            return Scaffold(body: Center(child: CircularProgressIndicator()));
          }
        },
      );
    },
  );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM