1

I am loading data from api (screeen 1) and on click of the item i am opening a new screen(screen 2) with new stateful widget. But now when i press back arrow from screen 2 i can see the screen 1 but it is calling api again. I do not want to that when we back from screen 2 to screen 1 .

Screen 1 code :

  class Screen1 extends StatefulWidget {
      @override
      _Screen1State createState() => _Screen1State();
    }

    class _Screen1 State
        extends State<Screen1> with AutomaticKeepAliveClientMixin<Screen1>
    {


      @override
      Widget build(BuildContext context) {
        return FutureBuilder(
            future: getdata(), //api calling
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.none:
                // TODO: Handle this case.
                case ConnectionState.waiting:
                  //my code
                case ConnectionState.active:
                // TODO: Handle this case.
                case ConnectionState.done:
                  if (snapshot.connectionState == ConnectionState.done) {
                    return showdata(snapshot.data);
                  }
              }
            });
      }

  Widget showdata(Response data) {
    return ListView.builder(
        itemCount: 1,
        itemBuilder: (BuildContext context, int index) {
          return Container(
              decoration: BoxDecoration(color: Colors.white),
              child: ListTile(
                onTap: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (_) => Screen2()));
                },
              )
          );
        });

      @override
      // TODO: implement wantKeepAlive
      bool get wantKeepAlive => true;
    }

Screen 2 :

class Screen2 extends StatefulWidget {
  @override
  _Screen2State createState() => _Screen2State();
}

class _Screen2State extends State<Screen2> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

How can i prevent to rebuild screen 1 again when i go back from screen 2 ?

  • im not sure but maybe global key will work with you – SUX Dec 15 '19 at 18:21
  • I marked it as duplicate of https://stackoverflow.com/questions/52249578/how-to-deal-with-unwanted-widget-build. Feel free to ping me if you disagree, I can potentially reopen it – Rémi Rousselet Dec 15 '19 at 18:33

0 Answers0