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

How to rebuild the entire page body after we get an HTTP response in async function

I would like to display a custom loading image right after a page loads, like the example in the first block of code below.

The loading screen should be visible until I get a response from async HTTP request, after which I want to replace / rebuild the entire body with a different widget and pass a variable from the getData() to the newly built body.

Please, don’t suggest to use the CircularProgressIndicator – I want to use a custom loading image.

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

The body after page load:

Widget build(BuildContext context) {
    return new Scaffold(
      body: Center(
        new Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('oading.gif'),
            ),
          ),
        ),
      ),
    );
}

The http request:

void getData() async {

      final response = await http.get(Uri.parse(URL));
      if (response.statusCode == 200) {
          messageText = "Success"; 
      } else {
          messageText = "Something went wrong"; 
      }
}

This is the resulting page body that I’d like to show after the getData() function loads:

Widget build(BuildContext context) {
  return new Scaffold(
      appBar: AppBar(),
      body: SingleChildScrollView(
        child: Container(
          child: Text(messageText),
        ),
      ),
  );
}

>Solution :

  • Use a stateful widget
  • create a boolean for loading set to false
  • in your get data method, set the state of the variable to true
  • on getting a 200, set the state to false

Something close to the below:

class MyWidget extends StatefulWidget {
  const MyWidget({Key? key}) : super(key: key);

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  bool isLoading = false;
  void getData() async {
    isLoading = true;
    if (mounted) setState(() {});
    final response = await http.get(Uri.parse(URL));
    if (response.statusCode == 200) {
      messageText = "Success";
    } else {
      messageText = "Something went wrong";
    }
    if (mounted) setState(() {});
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: isLoading ? null : AppBar(),
      body: isLoading
          ? Center(
              child: Container(
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('oading.gif'),
                  ),
                ),
              ),
            )
          : SingleChildScrollView(
              child: Container(
                child: Text(messageText),
              ),
            ),
    );
  }
}
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