Home > Mobile >  Flutter, refresh screen after await call
Flutter, refresh screen after await call

Time:06-01

Currently the only way I have to refresh the screen is using Navigator.push().

            if (response.statusCode == 200) {
              print(await response.stream.bytesToString());
              Navigator.push(
                  context,
                  new MaterialPageRoute(
                      builder: (context) => this.build(context)));
            }

But this only adds another screen to the stack of screens.

How can I refresh the screen after calling my print() without having to use Navigator.push()?

Thanks!

CodePudding user response:

You can set your class as Stateful Widget and use:

setState(() {
  // Add, edit or delete widgets
  // Set a diferent value for some variable
  // etc...
});

Or you can still use the Navigator but with the pushReplacement function, for example:

Navigator.pushReplacement(
    context,
    MaterialPageRoute(
        builder: (BuildContext context) => super.widget));

CodePudding user response:

use setState after request api response data,

setState is a way to dynamically change the UI.

It rebuild all current screen.

We call it inside the State Object class of the StatefulWidget.

  • Related