Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Show Widget for reload when timeout on StreamBuilder Flutter

I’m fetching data using a Future http request method and display everything using an StreamBuilder which just works fine. Now I want a reload widget to be shown when the requests times out. Currently my StreamBuilder looks like this:

StreamBuilder(
  stream: Stream.fromFuture(makeHttpRequest()),
  builder: (context, AsyncSnapshot<List<models.Tutorial>> snapshot) {
    if (snapshot.hasData) {
      // show requested data
    }
    return LoadingAnimationWidget.fourRotatingDots(
        color: const Color(0xFF465770), size: 45);
  });

I could add a timeout to the http request method or the stream itself but I wasn’t able to display a reload dialog on timeout. How could I implement that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You say that you know how to add a timeout to the Stream, which is already halfway to the solution:

stream: Stream.fromFuture(makeHttpRequest()).timeout(const Duration(seconds: TIME_OUT_IN_SECONDS),
            onTimeout: (controller) {
          controller.addError(ERROR_CAN_BE_ANY_OBJECT);
        }),

Then in your builder, you can check snapshot.hasError, then snapshot.error is the error that you’ve set in the onTimeout callback. If so, it suggests that a timeout has happened, and you can show your dialog.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading