skip to Main Content

I am building a mobile application that includes creating a drawer to switch between screens. Assuming I have 3 screens A, B, and C, how can I make it so that when I select screen A, it only loads data for the first time, and subsequent times I open screen A, it still retains the data without reloading? Similarly for screens B and C.

Are there ways to achieve this in Flutter? I really appreciate any help.

2

Answers


  1. Not sure which version of the flutter or which libs you are using for, but anyway I think you can do like this .Use AutomaticKeepAliveClientMixin

    class ScreenA extends StatefulWidget {
      @override
      _ScreenAState createState() => _ScreenAState();
    }
    
    class _ScreenAState extends State<ScreenA> with AutomaticKeepAliveClientMixin {
      // Add this line to preserve state
      @override
      bool get wantKeepAlive => true;
    
      // Your screen A code goes here
      // ...
      
      @override
      Widget build(BuildContext context) {
        super.build(context); // Don't forget to call super.build.
        return Scaffold(
          // ...
        );
      }
    }
    

    // same you will have for the B and c

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search